higher-order-functions

difference between foldLeft and reduceLeft in Scala

人盡茶涼 提交于 2019-11-29 19:03:25
I have learned the basic difference between foldLeft and reduceLeft foldLeft: initial value has to be passed reduceLeft: takes first element of the collection as initial value throws exception if collection is empty Is there any other difference ? Any specific reason to have two methods with similar functionality? Few things to mention here, before giving the actual answer: Your question doesn't have anything to do with left , it's rather about the difference between reducing and folding The difference is not the implementation at all, just look at the signatures. The question doesn't have

How can I explode and trim whitespace?

旧时模样 提交于 2019-11-29 18:54:04
For example, I would like to create an array from the elements in this string: $str = 'red, green, blue ,orange'; I know you can explode and loop through them and trim: $arr = explode(',', $str); foreach ($arr as $value) { $new_arr[] = trim($value); } But I feel like there's a one line approach that can handle this. Any ideas? You can do the following using array_map : $new_arr = array_map('trim', explode(',', $str)); An Improved answer preg_split ('/(\s*,*\s*)*,+(\s*,*\s*)*/', 'red, green thing ,, ,, blue ,orange'); Result: Array ( [0] => red [1] => green thing [2] => blue [3] => orange )

F# Higher-order property accessors

半腔热情 提交于 2019-11-29 15:10:35
I just upgraded my prototyping tuple to a record. Someday it may become a real class. In the meantime, I want to translate code like this: type Example = int * int let examples = [(1,2); (3,4); (5,6)] let descs = Seq.map (fst >> sprintf "%d") examples to this: type Example = { Field1 : int Field2 : int Description : string } let examples = [{Field1 = 1; Field2 = 2; Description = "foo"} {Field1 = 3; Field2 = 4; Description = "bar"} {Field1 = 5; Field2 = 6; Description = "baz"}] let descs = Seq.map Description examples The problem is that I expected to get a function Description : Example ->

Do I have to specify parameter names for higher-order function types in TypeScript?

*爱你&永不变心* 提交于 2019-11-29 14:46:13
问题 Trying to get my feet wet using TypeScript and I keep running into trouble. An old function resurfaced today and just as an exercise, I was curious if I could convert it to TypeScript. So far it's been a complete pain in the neck. declare type Ord = number | string; // type signature for f sucks really bad // (f: Ord => Ord => boolean) would be really nice, if possible // but instead I have to give names (_) for the parameters? dumb const arrayCompare = (f: (_: Ord) => (_: Ord) => boolean) =>

Function application function in Haskell

我与影子孤独终老i 提交于 2019-11-29 13:59:38
Let's say I have a list of functions functions = [f, g, h] each one with type a -> a I also have a list of values, say numbers but anything should work here vals = [1,2,3] I want to apply each function in functions to the corresponding value in vals My first instinct is to use a lambda and zipWith like: zipWith (\f v -> f v) functions vals But frankly this looks ugly and not something I'd expect in such a nice language like Haskell. A function application function sounds like the solution. Is there such a thing? Am I missing something and there is a much nicer solution to my problem? I

repeatedly applying a function until the result is stable

旧街凉风 提交于 2019-11-29 10:55:27
问题 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

How to use expand in snakemake when some particular combinations of wildcards are not desired?

被刻印的时光 ゝ 提交于 2019-11-28 14:28:22
Let's suppose that I have the following files, on which I want to apply some processing automatically using snakemake: test_input_C_1.txt test_input_B_2.txt test_input_A_2.txt test_input_A_1.txt The following snakefile uses expand to determine all the potential final results file: rule all: input: expand("test_output_{text}_{num}.txt", text=["A", "B", "C"], num=[1, 2]) rule make_output: input: "test_input_{text}_{num}.txt" output: "test_output_{text}_{num}.txt" shell: """ md5sum {input} > {output} """ Executing the above snakefile results in the following error: MissingInputException in line 4

Zip with default value instead of dropping values?

╄→гoц情女王★ 提交于 2019-11-28 10:53:10
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. You can append an inifinte list of 0 or 1 to each list and then take the number you need from the result

F# Higher-order property accessors

99封情书 提交于 2019-11-28 09:11:59
问题 I just upgraded my prototyping tuple to a record. Someday it may become a real class. In the meantime, I want to translate code like this: type Example = int * int let examples = [(1,2); (3,4); (5,6)] let descs = Seq.map (fst >> sprintf "%d") examples to this: type Example = { Field1 : int Field2 : int Description : string } let examples = [{Field1 = 1; Field2 = 2; Description = "foo"} {Field1 = 3; Field2 = 4; Description = "bar"} {Field1 = 5; Field2 = 6; Description = "baz"}] let descs = Seq

Function application function in Haskell

廉价感情. 提交于 2019-11-28 07:36:04
问题 Let's say I have a list of functions functions = [f, g, h] each one with type a -> a I also have a list of values, say numbers but anything should work here vals = [1,2,3] I want to apply each function in functions to the corresponding value in vals My first instinct is to use a lambda and zipWith like: zipWith (\f v -> f v) functions vals But frankly this looks ugly and not something I'd expect in such a nice language like Haskell. A function application function sounds like the solution. Is