我想创建一个类似的功能,以这种方式使用:
const objectCreator = createObject({
foo: functionFoo,
bar: functionBar
})
objectCreator(42) // => { foo: functionFoo(42), bar: functionBar(42) }
因此,想法是从应用于值的一组函数中创建一个对象。例如,用例是从一个唯一的对象中提取不同的数据,然后将此函数放入管道中。
我可以手动创建这样的功能,但是在Ramda(或类似产品)中是否已经不存在此功能?我不知道怎么命名。
答案 0 :(得分:4)
Map可以满足您的大部分需求,但前提是您要用代码包围起来以提供正确的值。例如:
--create some sample data that you gave
declare @inventory table ( PartNumber int,
Quantity int,
NewCount int,
diff int)
insert into @inventory
values
(2871451,1,0,1),
(4932615,6,1,5)
declare @PARTSREMAININGFIFO table ( id int,
PartNumber int,
PartDescription varchar(64),
Quantity int,
TotalPrice decimal (8,6),
brand varchar(64),
Location varchar(16),
Account varchar(64))
insert into @PARTSREMAININGFIFO
values
(9183,2871451,'AFM DEVICE GASKET',1,19.815225,'CUMMINS','A1','6015-Z'),
(9183,2871451,'AFM DEVICE GASKET',1,19.815225,'CUMMINS','A2','6015-Z'), --notice the extra (2nd) row here for part 2871451
(9183,2871451,'AFM DEVICE GASKET',1,19.815225,'CUMMINS','A3','6015-Z'), --notice the extra (3nd) row here for part 2871451
(3264,4932615,'GASKET',1,2.907144,'CUMMINS','A1','6015-Z'),
(9780,4932615,'GASKET',1,5.053475,'CUMMINS','A1','6015-Z'),
(9781,4932615,'GASKET',1,5.053475,'CUMMINS','A1','6015-Z'),
(9782,4932615,'GASKET',1,5.053475,'CUMMINS','A1','6015-Z'),
(9783,4932615,'GASKET',1,5.053475,'CUMMINS','A1','6015-Z'),
(9783,4932615,'GASKET',1,5.053475,'CUMMINS','A6','6015-Z') --notice the 6th (extra) row here for part 4932615
--staging table for your results
declare @tempResults table ( id int,
PartNumber int,
PartDescription varchar(64),
Quantity int,
TotalPrice decimal (8,6),
brand varchar(64),
Location varchar(16),
Account varchar(64))
declare cur cursor local fast_forward for
select distinct
PartNumber,
diff
from (select
PartNumber,
Quantity,
NewCount,
diff
from @inventory
where NewCount <> Quantity) x
declare @partNumber int
declare @diff int
open cur
fetch next from cur into @partNumber, @diff
while @@FETCH_STATUS = 0
begin
insert into @tempResults
select top (@diff)
id,
PartNumber,
PartDescription,
Quantity,
TotalPrice,
Brand,
Location,
Account
from @PARTSREMAININGFIFO
where PartNumber = @partNumber
order by Quantity --note you need to specify WHAT you want to order by
fetch next from cur into @partNumber, @diff
end
select *
from @tempResults
order by PartNumber
close cur
deallocate cur
或者如果您希望将其咖喱处理(因此您可以同时或分别传入规范和值):
const createObject = specification => value => R.map(f => f(value), specification);
const objectCreator = createObject({
foo: val => val * 2,
bar: val => val + 1,
});
let result = objectCreator(42); // { foo: 84, bar: 43 }
编辑:
如果输入的顺序颠倒了(即先输入值,后指定),则会更简单:
const createObject = R.curry((specification, value) => R.map(f => f(value), specification))
let result = createObject({
foo: val => val * 2,
bar: val => val + 1,
}, 42); // { foo: 84, bar: 43 }
答案 1 :(得分:2)
applySpec这样做:
const functionFoo = x => 'Foo: ' + x;
const functionBar = x => 'Bar: ' + x;
const objectCreator = applySpec({
foo: functionFoo,
bar: functionBar
});
objectCreator(42); // {"bar": "Bar: 42", "foo": "Foo: 42"}