higher-order-functions

Higher-order functions in Elisp

佐手、 提交于 2019-12-03 12:38:24
I created a function that returns a function in Elisp: (defun singleton-set (elem) (defun f (n) (= n elem)) f) I try to run this in IELM, and it fails: ELISP> (singleton-set 5) *** Eval error *** Symbol's value as variable is void: f ELISP> ((singleton-set 5) 5) *** Eval error *** Invalid function: (singleton-set 5) Due to What is the difference between Lisp-1 and Lisp-2? i changed the code to (defun singleton-set (elem) (defun f (n) (= n elem)) #'f) And invocation to (funcall (singleton-set 5) 5) , but now the error is *** Eval error *** Symbol's value as variable is void: elem I understand

Use of Clojure macros for DSLs

人走茶凉 提交于 2019-12-03 12:32:00
I am working on a Clojure project and I often find myself writing Clojure macros for DSLs, but I was watching a Clojure video of how a company uses Clojure in their real work and the speaker said that in practical use they do not use macros for their DSLs, they only use macros to add a little syntactic sugar. Does this mean I should write my DSL in using standard functions and then add a few macros at the end? Update : After reading the many varied (and entertaining) responses to this question I have realized that the answer is not as clear cut as I first thought, for many reasons: There are

What do we call this (new?) higher-order function?

冷暖自知 提交于 2019-12-03 12:31:59
问题 I am trying to name what I think is a new idea for a higher-order function. To the important part, here is the code in Python and Haskell to demonstrate the concept, which will be explained afterward. Python: >>> def pleat(f, l): return map(lambda t: f(*t), zip(l, l[1:])) >>> pleat(operator.add, [0, 1, 2, 3]) [1, 3, 5] Haskell: Prelude> let pleatWith f xs = zipWith f xs (drop 1 xs) Prelude> pleatWith (+) [0,1,2,3] [1,3,5] As you may be able to infer, the sequence is being iterated through,

How to use swift flatMap to filter out optionals from an array

一世执手 提交于 2019-12-03 10:55:12
问题 I'm a little confused around flatMap (added to Swift 1.2) Say I have an array of some optional type e.g. let possibles:[Int?] = [nil, 1, 2, 3, nil, nil, 4, 5] In Swift 1.1 I'd do a filter followed by a map like this: let filtermap = possibles.filter({ return $0 != nil }).map({ return $0! }) // filtermap = [1, 2, 3, 4, 5] I've been trying to do this using flatMap a couple ways: var flatmap1 = possibles.flatMap({ return $0 == nil ? [] : [$0!] }) and var flatmap2:[Int] = possibles.flatMap({ if

Swift subtle difference between curried and higher order function

风格不统一 提交于 2019-12-03 09:09:21
NOTE: This question was asked while Swift 2.1 was the latest. Given: class IntWrapper { var i: Int = 1 } func add(inout m: Int, i: Int) { m += i } And a higher order function func apply() -> (inout i: Int) -> () -> () { return { (inout i: Int) -> () -> () in return { add(&i, i: 1) } } } Application as so results in the member variable value never changing: var intW = IntWrapper() print(intW.i) // Prints '1' apply()(i: &intW.i)() print(intW.i) // Prints '1' However, when changing the function to curried form func apply()(inout i: Int)() -> () { add(&i, i: 1) } Application results in the member

Multiple folds in one pass using generic tuple function

末鹿安然 提交于 2019-12-03 06:52:26
How can I write a function which takes a tuple of functions of type ai -> b -> ai and returns a function which takes a tuple of elements of type ai , one element of type b , and combines each of the elements into a new tuple of ai : That is the signature should be like f :: (a1 -> b -> a1, a2 -> b -> a2, ... , an -> b -> an) -> (a1, a2, ... , an) -> b -> (a1, a2, ... , an) Such that: f (min, max, (+), (*)) (1,2,3,4) 5 = (1, 5, 8, 20) The point of this is so I can write: foldlMult' t = foldl' (f t) And then do something like: foldlMult' (min, max, (+), (*)) (head x, head x, 0, 0) x to do

Scope functions apply/with/run/also/let: Where do the names come from?

爱⌒轻易说出口 提交于 2019-12-03 05:14:01
问题 There are quite a few blog posts (like this) on usages of the standard library functions apply / with / run / also / let available that make it a bit easier to distingish when to actually use which of those pretty functions. For a few weeks now, the official docs even provide guidelines on that topic finally: https://kotlinlang.org/docs/reference/coding-conventions.html#using-scope-functions-applywithrunalsolet Nevertheless, I think it is pretty hard to memorize the function's individual use

What do we call this (new?) higher-order function?

孤者浪人 提交于 2019-12-03 02:55:59
I am trying to name what I think is a new idea for a higher-order function. To the important part, here is the code in Python and Haskell to demonstrate the concept, which will be explained afterward. Python: >>> def pleat(f, l): return map(lambda t: f(*t), zip(l, l[1:])) >>> pleat(operator.add, [0, 1, 2, 3]) [1, 3, 5] Haskell: Prelude> let pleatWith f xs = zipWith f xs (drop 1 xs) Prelude> pleatWith (+) [0,1,2,3] [1,3,5] As you may be able to infer, the sequence is being iterated through, utilizing adjacent elements as the parameters for the function you pass it, projecting the results into a

FoldLeft using FoldRight in scala

徘徊边缘 提交于 2019-12-03 01:58:29
问题 While going through Functional Programming in Scala, I came across this question: Can you right foldLeft in terms of foldRight? How about the other way around? In solution provided by the authors they have provided an implementation as follows: def foldRightViaFoldLeft_1[A,B](l: List[A], z: B)(f: (A,B) => B): B = foldLeft(l, (b:B) => b)((g,a) => b => g(f(a,b)))(z) def foldLeftViaFoldRight[A,B](l: List[A], z: B)(f: (B,A) => B): B = foldRight(l, (b:B) => b)((a,g) => b => g(f(b,a)))(z) Can

How to use swift flatMap to filter out optionals from an array

风流意气都作罢 提交于 2019-12-03 01:23:42
I'm a little confused around flatMap (added to Swift 1.2) Say I have an array of some optional type e.g. let possibles:[Int?] = [nil, 1, 2, 3, nil, nil, 4, 5] In Swift 1.1 I'd do a filter followed by a map like this: let filtermap = possibles.filter({ return $0 != nil }).map({ return $0! }) // filtermap = [1, 2, 3, 4, 5] I've been trying to do this using flatMap a couple ways: var flatmap1 = possibles.flatMap({ return $0 == nil ? [] : [$0!] }) and var flatmap2:[Int] = possibles.flatMap({ if let exercise = $0 { return [exercise] } return [] }) I prefer the last approach (because I don't have to