higher-order-functions

Swift higher order function (Church pair aka cons) with generic parameter types not accepting input parameter types

℡╲_俬逩灬. 提交于 2019-12-01 08:50:16
I was messing around with the functional programming in Swift 2.1, trying to implement the Church encoding pair/cons function ( cons = λx λy λf f x y in untyped lambda calculus ), which I had read couldn't be done in earlier versions of Swift. With generics it looks like func cons<S,T,U>(x:S,_ y:T) -> ((S,T) -> U) -> U { return { (f:((S,T) -> U)) -> U in return f(x,y)} } cons(1,2) //error: cannot invoke 'cons' with an argument list of type '(Int, Int)' //note: expected an argument list of type '(S, T)' which doesn't work, and gives an error I cannot understand (surely parameter list of type

Why does map/filter … not work with an Array of Nothing?

霸气de小男生 提交于 2019-12-01 07:10:21
问题 Isn't Nothing a subtype of all types? scala> val array = new Array(5) array: Array[Nothing] = Array(null, null, null, null, null) scala> array.map(_ => 42) <console>:9: error: value map is not a member of Array[Nothing] array.map(_ => 42) ^ scala> array.filter(_ != 42) <console>:9: error: value filter is not a member of Array[Nothing] array.filter(_ != 42) ^ It's weird that this doesn't work. Is this specified, a feature or a bug? 回答1: When you see weird behavior involving Nothing, it's

Swift higher order function (Church pair aka cons) with generic parameter types not accepting input parameter types

故事扮演 提交于 2019-12-01 06:18:57
问题 I was messing around with the functional programming in Swift 2.1, trying to implement the Church encoding pair/cons function (cons = λx λy λf f x y in untyped lambda calculus), which I had read couldn't be done in earlier versions of Swift. With generics it looks like func cons<S,T,U>(x:S,_ y:T) -> ((S,T) -> U) -> U { return { (f:((S,T) -> U)) -> U in return f(x,y)} } cons(1,2) //error: cannot invoke 'cons' with an argument list of type '(Int, Int)' //note: expected an argument list of type

How to return member values in a array of objects using lambda expressions

∥☆過路亽.° 提交于 2019-12-01 00:39:10
I have an array of "Dog" where i want to print the name of all dogs older then 5 years. I tried something like Dogs.filter{ it.age > 5 }.forEach { it.name } This gives me the value i need, but how do I store and return it as a list of strings? I tried things like adding .join(",") but since I don't get any array in return it wont work. I think you're looking for the map operator: val dogNames: List<String> = dogs.filter { it.age > 5 }.map { it.name } 来源: https://stackoverflow.com/questions/49427606/how-to-return-member-values-in-a-array-of-objects-using-lambda-expressions

What is a general scheme for writing a function in pointfree style?

不打扰是莪最后的温柔 提交于 2019-12-01 00:30:27
问题 I am working through the 20 Intermediate Haskell Exercises at the moment, which is quite a fun exercise. It involves implementing various instances of the typeclasses Functor and Monad (and functions that takes Functor s and Monad s as arguments) but with cute names like Furry and Misty to disguise what we're doing (makes for some interesting code). I've been trying to do some of this in a point-free style, and I wondered if there's a general scheme for turning a point-ful (?) definition into

Promises in lapply / R

旧街凉风 提交于 2019-11-30 21:40:52
I am not sure what the promises are doing in R If one runs a = lapply(seq_len(2), function(n) { function() {n}}) b = lapply(seq_len(2), function(n) {n}) we can see that a[[1]]() # == 2 b[[1]] # == 1 I understand that R uses promise's object and lazily evaluates an expression in its environment, but I dont understand why the different environments created for each function would not contain their own value for n. [[1]] function () { n } <environment: 0x7f9b2416ad18> [[2]] function () { n } <environment: 0x7f9b2416ab20> as.list(environment(a[[1]])) $n [1] 2 as.list(environment(a[[2]])) $n [1] 2

Locking on an interned string?

旧街凉风 提交于 2019-11-30 14:39:09
问题 Update: It is acceptable if this method is not thread safe, but I'm interested in learning how I would make it thread safe. Also, I do not want to lock on a single object for all values of key if I can avoid it. Original Question: Suppose I want to write a higher order function that takes a key and a function, and checks if an object has been cached with the given key. If is has, the cached value is returned. Otherwise, the given function is run and the result is cached and returned. Here's a

Locking on an interned string?

痴心易碎 提交于 2019-11-30 11:37:54
Update: It is acceptable if this method is not thread safe, but I'm interested in learning how I would make it thread safe. Also, I do not want to lock on a single object for all values of key if I can avoid it. Original Question: Suppose I want to write a higher order function that takes a key and a function, and checks if an object has been cached with the given key. If is has, the cached value is returned. Otherwise, the given function is run and the result is cached and returned. Here's a simplified version of my code: public static T CheckCache<T>(string key, Func<T> fn, DateTime expires)

repeatedly applying a function until the result is stable

本秂侑毒 提交于 2019-11-30 08:02:52
I want to repeatedly apply a function simplify' until the result is "stable" (i.e. simplify'(x) == x ): simplify :: Expr -> Expr simplify expr = let iterations = iterate simplify' expr neighbours = zip iterations (tail iterations) simplified = takeWhile (\(a, b) -> a /= b) neighbours in snd $ last ((expr, expr) : simplified) simplify' :: Expr -> Expr This seems to be a common problem to me. Is there a more elegant solution? Update: I found a much simpler solution, but I'm still looking for a more elegant solution :) simplify expr = let next = simplify' expr in if next == expr then expr else

PHP array function that returns a subset for given keys

余生长醉 提交于 2019-11-30 00:19:04
问题 I'm looking for an array function that does something like this: $myArray = array( 'apple'=>'red', 'banana'=>'yellow', 'lettuce'=>'green', 'strawberry'=>'red', 'tomato'=>'red' ); $keys = array( 'lettuce', 'tomato' ); $ret = sub_array($myArray, $keys); where $ret is: array( 'lettuce'=>'green', 'tomato'=>'red' ); A have no problem in writing it down by myself, the thing is I would like to avoid foreach loop and adopt a built-in function or a combination of built-in functions. It seems to me