functional-programming

How are parameters handled when passing functions in Javascript? [duplicate]

こ雲淡風輕ζ 提交于 2019-12-23 09:17:45
问题 This question already has answers here : Higher-order functions in Javascript (4 answers) Closed 4 years ago . In http://eloquentjavascript.net/1st_edition/chapter6.html, there is the following example: function negate(func) { return function(x) { return !func(x); }; } var isNotNaN = negate(isNaN); alert(isNotNaN(NaN)); Knowing only basic Javascript and imperative programming, I am stumped by this programming style. Can someone help me to understand what happens during runtime. I stepped

Pattern matching based on the function signature

对着背影说爱祢 提交于 2019-12-23 09:17:31
问题 In F# can you pattern match on a function signature. I want to decorate a number of functions with a function that measures the execution of the function and calls out to statsd. The current function I have is: let WrapFunctionWithPrefix(metrics:Metric.Client.IRecorder, functionToWrap, prefix) = let metricsIdentifier = (sprintf "%s.%s" prefix Environment.MachineName) using (metrics.StartTimer(metricsIdentifier)) ( fun metrics -> functionToWrap) As you can see above, the prefix will vary, and

last element in list using ocaml List.fold_left

好久不见. 提交于 2019-12-23 09:17:12
问题 I can find the last element of a list by the following code. let last (xs:'a list) : 'a = let rec aux xs prev = match xs with | [] -> prev | x::ys -> aux ys x in match xs with | [] -> failwith "no element" | x::xs -> aux xs x How do I find the last element of the same list using the List.fold_left function in OCaml? Thanks in advance! 回答1: fold_left accesses the list from the head to the tail, thus the function passed to fold_left should just replace the accumulator with the current element

How to return the index of a for loop in OCaml?

霸气de小男生 提交于 2019-12-23 08:38:39
问题 let find_free_next heap start = for i = start to ((Array.length heap)-1) do match heap.(i) with Hdr (Free (h), g) -> i done How can i return the index of a loop as an integer once the match has been found? 回答1: If you want to stick to the imperative style, you can use an exception to exit the loop: exception Found of int let find_free_next heap start = try for i = start to Array.length heap - 1 do match heap.(i) with | Hdr (Free (h), g) -> raise (Found i) | _ -> () (* If it is not what you

How does Clojure's laziness interact with calls to Java/impure code?

拥有回忆 提交于 2019-12-23 07:52:35
问题 We stumbled upon an issue in our code today, and couldn't answer this Clojure question: Does Clojure evaluate impure code (or calls to Java code) strictly or lazily? It seems that side-effects + lazy sequences can lead to strange behavior. Here's what we know that led to the question: Clojure has lazy sequences: user=> (take 5 (range)) ; (range) returns an infinite list (0 1 2 3 4) And Clojure has side-effects and impure functions: user=> (def value (println 5)) 5 ; 5 is printed out to screen

Parentheses matching in Scala — functional approach

僤鯓⒐⒋嵵緔 提交于 2019-12-23 07:48:33
问题 Let's say I want to parse a string with various opening and closing brackets (I used parentheses in the title because I believe it is more common -- the question is the same nevertheless) so that I get all the higher levels separated in a list. Given: [hello:=[notting],[hill]][3.4(4.56676|5.67787)][the[hill[is[high]]not]] I want: List("[hello:=[notting],[hill]]", "[3.4(4.56676|5.67787)]", "[the[hill[is[high]]not]]") The way I am doing this is by counting the opening and closing brackets and

Parentheses matching in Scala — functional approach

浪子不回头ぞ 提交于 2019-12-23 07:48:05
问题 Let's say I want to parse a string with various opening and closing brackets (I used parentheses in the title because I believe it is more common -- the question is the same nevertheless) so that I get all the higher levels separated in a list. Given: [hello:=[notting],[hill]][3.4(4.56676|5.67787)][the[hill[is[high]]not]] I want: List("[hello:=[notting],[hill]]", "[3.4(4.56676|5.67787)]", "[the[hill[is[high]]not]]") The way I am doing this is by counting the opening and closing brackets and

zip function in Racket/Scheme

隐身守侯 提交于 2019-12-23 07:47:14
问题 Given two lists, return a list whose elements are lists of size two, such that for the i -th list, the first element is the i -th element of the first original list, and the second element is the i -th element of the second original list. If one list is smaller than the other, the resulting list is of the smallest size; and so if one of the lists is empty, return an empty list. For example: > (zip '(1 2) '(3 4)) '((1 3) (2 4)) > (zip '(1 2 3) '()) '() > (zip '() '(4 5 6)) '() > (zip '(8 9) '

Scala insert into list at specific locations

核能气质少年 提交于 2019-12-23 07:39:00
问题 This is the problem that I did solve, however being a total imperative Scala noob, I feel I found something totally not elegant. Any ideas of improvement appreciated. val l1 = 4 :: 1 :: 2 :: 3 :: 4 :: Nil // original list val insert = List(88,99) // list I want to insert on certain places // method that finds all indexes of a particular element in a particular list def indexesOf(element:Any, inList:List[Any]) = { var indexes = List[Int]() for(i <- 0 until inList.length) { if(inList(i) ==

How can I idiomatically “remove” a single element from a list in Scala and close the gap?

谁都会走 提交于 2019-12-23 07:26:50
问题 Lists are immutable in Scala, so I'm trying to figure out how I can "remove" - really, create a new collection - that element and then close the gap created in the list. This sounds to me like it would be a great place to use map, but I don't know how to get started in this instance. Courses is a list of strings. I need this loop because I actually have several lists that I will need to remove the element at that index from (I'm using multiple lists to store data associated across lists, and