currying

scala currying/partials to build function filter list

你。 提交于 2020-01-04 09:06:10
问题 Given the following code: case class Config( addThree: Boolean = true, halve: Boolean = true, timesFive: Boolean = true ) def doOps(num: Integer, config: Config): Integer = { var result: Integer = num if ( config.addThree ) { result += 3 } if ( config.halve ) { result /= 2 } if ( config.timesFive ) { result *= 5 } result } val config = Config(true,false,true) println( doOps(20, config) ) println( doOps(10, config) ) I'd like to replace the ugly doOps method with a more efficient and idiomatic

scala currying/partials to build function filter list

纵然是瞬间 提交于 2020-01-04 09:06:10
问题 Given the following code: case class Config( addThree: Boolean = true, halve: Boolean = true, timesFive: Boolean = true ) def doOps(num: Integer, config: Config): Integer = { var result: Integer = num if ( config.addThree ) { result += 3 } if ( config.halve ) { result /= 2 } if ( config.timesFive ) { result *= 5 } result } val config = Config(true,false,true) println( doOps(20, config) ) println( doOps(10, config) ) I'd like to replace the ugly doOps method with a more efficient and idiomatic

What does 'int ?. tree' mean in an SML error message?

怎甘沉沦 提交于 2020-01-04 05:14:06
问题 I have the following SML code that I wrote for a class: fun lookup (cmp: 'a * 'a -> order) (x: 'a, t: 'a tree) : 'a option = case t of Empty => NONE | Node(l,y,r) => case cmp(x,y) of EQUAL => SOME y | LESS => lookup (cmp) (x,r) | GREATER => lookup (cmp) (x,l) In testing this with: val SOME 3 = lookup Int.compare (3, Node(Empty,3,Empty)); And getting the following error back: stdIn:153.1-166.12 Error: operator and operand don't agree [tycon mismatch] operator domain: int * int ?.tree operand:

what are curry and uncurry in high-order functions in ML

一个人想着一个人 提交于 2020-01-02 04:01:25
问题 fun curry f x y = f (x, y); fun uncurry f (x, y) = f x y; fun compose (f, g) x = f (g x); I understand compose function, but not quite understand curry and uncurry in ML. Can anyone explain these? Also, what do the following two lines mean? (1) compose (compose, uncurry compose) (2) compose (uncurry compose, compose) 回答1: If you look at the types, then you will clearly see what curry and uncurry does. Remember that it is possible to define function which either takes its arguments as one big

Partial application with a C++ lambda?

末鹿安然 提交于 2019-12-31 13:55:32
问题 EDIT: I use curry below, but have been informed this is instead partial application. I've been trying to figure out how one would write a curry function in C++, and i actually figured it out! #include <stdio.h> #include <functional> template< class Ret, class Arg1, class ...Args > auto curry( Ret f(Arg1,Args...), Arg1 arg ) -> std::function< Ret(Args...) > { return [=]( Args ...args ) { return f( arg, args... ); }; } And i wrote a version for lambdas, too. template< class Ret, class Arg1,

Currying Expressions in C#

筅森魡賤 提交于 2019-12-30 09:53:28
问题 I am trying to build up an expression tree that I can feed into Linq2SQL so that it will generate a nice clean query. My purpose is to build a filter that takes an arbitrary set of words to AND and NOT (or OR and NOT) together. Because I want to vary the fields that I search on I preferably want to compose a list of Expresssion<Func<T, string, bool>> 's together (where T is the entity I am operating on) by calling a variety of helper functions. Then I would receive an array of words and loop

Currying Expressions in C#

给你一囗甜甜゛ 提交于 2019-12-30 09:53:08
问题 I am trying to build up an expression tree that I can feed into Linq2SQL so that it will generate a nice clean query. My purpose is to build a filter that takes an arbitrary set of words to AND and NOT (or OR and NOT) together. Because I want to vary the fields that I search on I preferably want to compose a list of Expresssion<Func<T, string, bool>> 's together (where T is the entity I am operating on) by calling a variety of helper functions. Then I would receive an array of words and loop

Scala: curried constructors

喜夏-厌秋 提交于 2019-12-29 06:41:13
问题 I have the following Scala class: class Person(var name : String, var age : Int, var email : String) I would like to use the Person constructor as a curried function: def mkPerson = (n : String) => (a : Int) => (e : String) => new Person(n,a,e) This works, but is there another way to accomplish this? This approach seems a bit tedious and error-prone. I could imagine something like Function.curried, but then for constructors. 回答1: This will work: def mkPerson = (new Person(_, _, _)).curried

What does uncurry ($) do?

隐身守侯 提交于 2019-12-29 05:18:33
问题 I'm doing some excersises where I have to add a function's type and explain what it does. I'm stuck with this: phy = uncurry ($) The type, according to GHCi is phy :: (a -> b, a) -> b . My haskell knowledge is basic so I really have no idea what it does. 回答1: Let's spell out the type part systematically. We'll start with the types of uncurry and ($) : uncurry :: (a -> b -> c) -> (a, b) -> c ($) :: (a -> b) -> a -> b Since the target expression has ($) as the argument of uncurry , let's line

“Uncurrying” an instance method in .NET

杀马特。学长 韩版系。学妹 提交于 2019-12-28 11:56:22
问题 Can you create a delegate of an instance method without specifying the instance at creation time? In other words, can you create a "static" delegate that takes as it's first parameter the instance the method should be called on? For example, how can I construct the following delegate using reflection? Func<int, string> = i=>i.ToString(); I'm aware of the fact that I can use methodInfo.Invoke, but this is slower, and does not check for type-correctness until it is called. When you have the