currying

Currying in hardware

℡╲_俬逩灬. 提交于 2020-01-24 15:45:09
问题 I'm trying to implement a simple Address Decoder with a curried function inside. The code below won't compile, could anybody help me with this? class AddrDecoder[T<:UInt] (dType:T, n:Int) extends Module { val io = IO (new Bundle { //val range = (Vec(Seq.fill(n){(dType,dType)})) // This won't compile, how to fix ? val range = (List.fill(n){(dType,dType)}) val addr = Input (dType) val en = Input (Bool()) val sel = Output(Bool()) }) def inside (range:(T,T))(addr:T):Bool = { addr >= range._1 &&

Currying out of order in Haskell

南笙酒味 提交于 2020-01-22 13:35:07
问题 Is there an elegant notation for Currying the arguments of a function out of order in Haskell? For example, if you wish to divide 2 by all elements of a list, you can write map ((/) 2) [1,2,3,4,5] However to divide all elements of a list it seems you need to define an anonymous function map (\x -> x/2) [1,2,3,4,5] Anonymous functions quickly become unwieldy in more complex cases. I'm aware that in this case map ((*) 0.5) [1,2,3,4,5] would work fine, but I'm interested to know if Haskell has a

In currying why does the outside, initiating function appear to be the innermost function?

*爱你&永不变心* 提交于 2020-01-16 19:02:07
问题 In the simple, 2 layer currying example below the word dog is passed into the y argument. "use strict"; function layer2(x) { return function(y) { return console.log(x, y); } } var layer1 = layer2('cat'); layer1('dog'); I know it works this way and I can reliably reproduce this. However I can't understand the mechanics of why this works. If I made a 100 layer example layer 1 would be the innermost function and the pattern would work outwards from that point. What piece of basic mechanics

Generalised Curry - Javascript

烈酒焚心 提交于 2020-01-14 19:05:27
问题 While reading an article on implementation of a generalised curry in Javascript, I stumbled upon this piece of code. function curry(fn) { return (...xs) => { if (xs.length === 0) { throw Error('EMPTY INVOCATION'); } if (xs.length >= fn.length) { return fn(...xs); } return curry(fn.bind(null, ...xs)); }; } I am unable to grok the part of the explanation which states We create a copy of fn that has the first k arguments bound (partially applied) and pass it to curry as the next fn, with its

Is currying just a way to avoid inheritance?

白昼怎懂夜的黑 提交于 2020-01-13 08:21:07
问题 So my understanding of currying (based on SO questions) is that it lets you partially set parameters of a function and return a "truncated" function as a result. If you have a big hairy function takes 10 parameters and looks like function (location, type, gender, jumpShot%, SSN, vegetarian, salary) { //weird stuff } and you want a "subset" function that will let you deal with presets for all but the jumpShot% , shouldn't you just break out a class that inherits from the original function? I

How do you curry the 2nd (or 3rd, 4th, …) parameter in F# or any functional language?

房东的猫 提交于 2020-01-12 13:47:36
问题 I'm just starting up with F# and see how you can use currying to pre-load the 1st parameter to a function. But how would one do it with the 2nd, 3rd, or whatever other parameter? Would named parameters to make this easier? Are there any other functional languages that have named parameters or some other way to make currying indifferent to parameter-order? 回答1: Typically you just use a lambda: fun x y z -> f x y 42 is a function like 'f' but with the third parameter bound to 42. You can also

Advantages of using curried functions over a normal function in javascript

南笙酒味 提交于 2020-01-11 12:51:14
问题 Below is a specific use case of using a normal and a curried function. Are there any advantages for using either if you only using two arguments? //Normal Function function add(x, y) { return x + y; } //Curried Function function add1(x) { return function add2(y) { return x + y; } } 回答1: Here's a small example: let add = (x, y) => x + y; let addc = x => y => x + y; // add 5 to every element result = [1,2,3,4,5].map(x => add(x, 5)) // dirty and tedious result = [1,2,3,4,5].map(addc(5)) // nice

How to curry a … argument by position in R?

谁都会走 提交于 2020-01-11 04:54:08
问题 This may fall under "you can't, and there's no reason to anyway," but I'm curious if it's possible. At very least, maybe it will be a fun R puzzle. I was pondering currying cat to always append \n . However, cat is written so that it pastes together as many arguments as it is given (via ... ). Surprisingly, this works: > library(functional) > catnip <- Curry( cat, "\n" ) > catnip("hi") hi However, the \n winds up before the user's text. Is there any way to curry the function such that you

Ruby rcurry. How I can implement proc “right” currying?

别等时光非礼了梦想. 提交于 2020-01-06 15:04:12
问题 I'm working in a new gem to extend the Ruby Core classes, something similar to Active Support Core Extensions or Powerpack. These days I'm working on the Proc class, for example I added closure composition. But today I like to talk about currying. This is the Ruby standard library functionality for curry : describe "#curry" do it "returns a curried proc" do modulus = ->(mod, num) { num % mod } mod2 = modulus.curry[2] expect(mod2.call(5)).to eq(1) end end I like to add a new rcurry method to

Unable to understand placeholder _ behavior in scala function application

╄→尐↘猪︶ㄣ 提交于 2020-01-05 03:06:29
问题 Welcome to Scala version 2.10.2 Type in expressions to have them evaluated. Type :help for more information. scala> val fn = (x:Int) => x+1 fn: Int => Int = <function1> scala> val fn1 = fn _ fn1: () => Int => Int = <function0> scala> val fn2 = fn1 _ fn2: () => () => Int => Int = <function0> I don't understand why the placeholder(without a suggested type) application to a function is creating a new curried function with prefixed additional void argument. I was expecting a compiler error or at