functional-programming

Javascript Function.prototype.call()

旧巷老猫 提交于 2019-12-17 20:30:25
问题 I read some article and it said the following 2 line are doing the same thing. fn.call(thisValue); Function.prototype.call.call(fn, thisValue); For line 1, my understanding is that every function object in Javascript do have a the method call inherited from the Function.prototype and what call method does is to have the this keyword inside the function definition of fn to be thisValue (the first parameter I passed in the call method. fn is a function so what I am doing in fn.call(thisValue)

Underscores in a Scala map/foreach

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-17 20:26:47
问题 Can you please help me understand what the underscore is doing in the second case below? I guess it's defining an anonymous function for each element of the list, but why is that function not being called like it is in the first case? scala> List(1,2,3,4).foreach(x => println("*" * x)) * ** *** **** scala> List(1,2,3,4).foreach(_ => println("*" * _)) $line25.$read$$iw$$iw$$iw$$iw$$$Lambda$1197/562203102@a632ae0 $line25.$read$$iw$$iw$$iw$$iw$$$Lambda$1197/562203102@a632ae0 $line25.$read$$iw$

Apply several string transformations in scala

↘锁芯ラ 提交于 2019-12-17 19:48:10
问题 I want to perform several ordered and successive replaceAll(...,...) on a string in a functional way in scala. What's the most elegant solution ? Scalaz welcome ! ;) 回答1: First, let's get a function out of the replaceAll method: scala> val replace = (from: String, to: String) => (_:String).replaceAll(from, to) replace: (String, String) => String => java.lang.String = <function2> Now you can use Functor instance for function, defined in scalaz. That way you can compose functions, using map (or

Where can I find an explanation/summary of symbols used to explain functional programming, specifically Ramda.js?

我的未来我决定 提交于 2019-12-17 19:46:29
问题 The API documentation for the JavaScript functional programming library Ramda.js contains symbolic abbreviations but does not provide a legend for understanding these. Is there a place (website, article, cheatsheet, etc.) that I can go to to decipher these? Some examples from the Ramda.js API documentation: Number -> Number -> Number Apply f => f (a -> b) -> f a -> f b Number -> [a] -> [[a]] (*... -> a) -> [*] -> a {k: ((a, b, ..., m) -> v)} -> ((a, b, ..., m) -> {k: v}) Filterable f => (a ->

What is the simplest way to access data of an F# discriminated union type in C#?

心不动则不痛 提交于 2019-12-17 19:36:13
问题 I'm trying to understand how well C# and F# can play together. I've taken some code from the F# for Fun & Profit blog which performs basic validation returning a discriminated union type: type Result<'TSuccess,'TFailure> = | Success of 'TSuccess | Failure of 'TFailure type Request = {name:string; email:string} let TestValidate input = if input.name = "" then Failure "Name must not be blank" else Success input When trying to consume this in C#; the only way I can find to access the values

Zip with default value instead of dropping values?

℡╲_俬逩灬. 提交于 2019-12-17 19:33:24
问题 I'm looking for a function in haskell to zip two lists that may vary in length. All zip functions I could find just drop all values of a lists that is longer than the other. For example: In my exercise I have two example lists. If the first one is shorter than the second one I have to fill up using 0's. Otherwise I have to use 1's. I'm not allowed to use any recursion. I just have to use higher order functions. Is there any function I can use? I really could not find any solution so far. 回答1:

Implementing a tail recursive version of quicksort-like function in F#/OCaML

故事扮演 提交于 2019-12-17 18:46:01
问题 Is it possible to implement a tail recursive version of the quick sort algorithm (via the continuation pattern)? And if it is, how would one implement it? Normal (not optimized) version: let rec quicksort list = match list with | [] -> [] | element::[] -> [element] | pivot::rest -> let ``elements smaller than pivot``, ``elements larger or equal to pivot``= rest |> List.partition(fun element -> element < pivot) quicksort ``elements smaller than pivot`` @ [pivot] @ quicksort ``elements larger

Why are “pure” functions called “pure”? [closed]

早过忘川 提交于 2019-12-17 18:45:46
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 years ago . A pure function is one that has no side effects -- it cannot do any kind of I/O and it cannot modify the state of anything -- and it is referentially transparent -- when called multiple times with the same inputs, it always gives the same outputs. Why is the word "pure" used to describe functions with those

In Functional Programming, is it considered a bad practice to have incomplete pattern matchings

时光总嘲笑我的痴心妄想 提交于 2019-12-17 18:33:12
问题 Is it generally considered a bad practice to use non-exhaustive pattern machings in functional languages like Haskell or F#, which means that the cases specified don't cover all possible input cases? In particular, should I allow code to fail with a MatchFailureException etc. or should I always cover all cases and explicitly throw an error if necessary? Example: let head (x::xs) = x Or let head list = match list with | x::xs -> x | _ -> failwith "Applying head to an empty list" F# (unlike

How much is there to LINQ?

泄露秘密 提交于 2019-12-17 17:52:08
问题 I'm looking into LINQ and the query language appears (at least on the surface) to be nothing more than an implementation of map and/or list comprehensions as found in Haskell and other FP languages (particularly the generalisation of 'map' and 'for' in Scala). Is this correct? Is there more to the syntax than this? From the breathless tone of the book I'm reading ("Essential LINQ") it would seem like there's something new or innovative here. There's the whole back-end, pipeline, first-order