functional-programming

Java 8 Stream unique characters from array

孤人 提交于 2019-12-25 04:49:09
问题 Trying to write a simple program that will print the unique words from an input array in Java8 . For example if the input is String[] input = {"This", "is", "This", "not"}; The program should output [T, h, i, s, n, o, t] , the order of elements should follow the same pattern as they appear in input. My approach is to split the input , then map , distinct and finally collect it toList. But the following code is printing list of streams instead of words, what am i missing?.E.g. String[] input =

Primitive recursion

一个人想着一个人 提交于 2019-12-25 04:33:41
问题 Merged with How can I simplify a basic arithmetic expression?. how will i define the function 'simplify' using primitive recursion? simplify :: Expr -> Expr ... simplify Simplify an expression using basic arithmetic, e.g. simplify (Plus (Var "x") (Const 0)) = Var "x" 来源: https://stackoverflow.com/questions/324003/primitive-recursion

When would I write a non-tail recursive function in Scala?

浪子不回头ぞ 提交于 2019-12-25 02:30:01
问题 Since non-tail recursion calls use stack frames like Java does, I'd think you'd be using it very sparingly, if at all. This seems however severely restrictive given it's one of the most important tools. When can I use non-tail recursion functions? Also, are there plans to remove the memory restriction in the future? 回答1: In the same situations where it would be safe in Java, where the dataset you are working with never grows huge and the performance isn't critical/hot path of your app. Also,

<functional> (nested bind) problems with MSVC 2010

邮差的信 提交于 2019-12-25 02:23:10
问题 I have the following code (I'm sorry for the lengthiness): double primeValue( const func1D &func, const double lowerBound, const double upperBound, const double pole ) { // check bounds if( lowerBound >= upperBound ) throw runtime_error( "lowerBound must be smaller than upperBound!" ); // C++0x way of writing: fullFunc(x) = func(x)/(x-a) func1D fullFunc = bind( divides<double>(), // division of bind(func, _1), // f(x), with _1 = x bind(minus<double>(), _1, pole) ); // by x-a, with _1 = x //

Scala - List[MyObject] -> Map[Long, List[Long]]

瘦欲@ 提交于 2019-12-25 01:46:16
问题 I'm very new to scala and am trying to achieve the following: I have a list of objects like so: groups = [{id=1, iq=2}, {id=1,iq=3}, {id=2, iq=4}] How can build a map[Long,List[Long]] from groups? eg. [{1, [2,3]}, {2,[4]}] Any suggestions would be very much appreciated :) 回答1: You can use the groupBy method. Here's an example in a REPL session: scala> case class MyObject(id: Long, iq: Long) defined class MyObject scala> val xs = List(MyObject(1, 2), MyObject(1, 3), MyObject(2, 4)) xs: List

How to define the partitions (factorizations w.r.t. concatenation) of a sequence as a lazy sequence of lazy sequences in Clojure

倾然丶 夕夏残阳落幕 提交于 2019-12-25 01:39:56
问题 I am new to Clojure and I want to define a function pt taking as arguments a number n and a sequence s and returning all the partitions of s in n parts, i.e. its factorizations with respect to n -concatenation. for example (pt 3 [0 1 2]) should produce: (([] [] [0 1 2]) ([] [0] [1 2]) ([] [0 1] [2]) ([] [0 1 2] []) ([0] [] [1 2]) ([0] [1] [2]) ([0] [1 2] []) ([0 1] [] [2]) ([0 1] [2] []) ([0 1 2] [] [])) with the order being unimportant. Specifically, I want the result to be a lazy sequence

Speeding up binary tree traversal with multiple processors Haskell (parallel)

你离开我真会死。 提交于 2019-12-25 01:22:22
问题 Following the examples in Chapter 24 of "Real World Haskell" and Chapter 2 of "Parallel and Concurrent Programming in Haskell", I've managed to build the following functions for building and traversing a binary tree quickly using multiple processors. import Control.Parallel (par, pseq) import Control.DeepSeq data Tree x = Empty | Node x (Tree x) (Tree x) deriving (Show, Read, Eq) -- Create instance of NFData for Tree data type (defining "normal form") instance NFData a => NFData (Tree a)

tree-fold defintion that works both with + and append

亡梦爱人 提交于 2019-12-25 01:09:22
问题 (define (tree-fold f tree) (if (pair? tree) (apply f (car tree) (map (lambda (t) (tree-fold f t)) (cdr tree))) (f tree))) works for example with: (tree-fold + '(1 (2 2)(2 2)) -> 9 However if I want to use (tree-fold append '(1 (2 2)(2 2))) , I have to modify the tree-fold with list around (car tree) , which breaks it for + . Is there some mechanism that can be used in the tree-fold definition that would make it work with both + and append ? 回答1: This should work, adding one parameter to

Why is Elixir's Enum.count not returning a number? [duplicate]

杀马特。学长 韩版系。学妹 提交于 2019-12-24 23:19:49
问题 This question already has answers here : Elixir lists interpreted as char lists (2 answers) Closed last year . I have the following input: [ [ {"title", "Aaaaaaaaa aaaaaaaaaaa aa aaa oaaaaaaaa"}, {"title", "Aaaaaaa aa aaaa aaa.aa"}, {"title", "Aaaaaaaaaaaa Aaaaaaa, aaa Aaaaaaa"}, {"title", "Aaaa Aaaaaaaa aaaa Aaaaa"}, {"title", "Aaaaaaa Aaaaaaa aaaa Aaaaa"}, {"title", "Aaaaa Aaaaaaa aa Aaaaaa"}, {"title", "Aaaaaaaaa Aaaaa Aaaa Aaaaaaa, Aaa Aaaaaaa"}, {"title", "Aaaaaaaaa Aaaaaaa Aaaaaa

Why is Elixir's Enum.count not returning a number? [duplicate]

故事扮演 提交于 2019-12-24 22:42:04
问题 This question already has answers here : Elixir lists interpreted as char lists (2 answers) Closed last year . I have the following input: [ [ {"title", "Aaaaaaaaa aaaaaaaaaaa aa aaa oaaaaaaaa"}, {"title", "Aaaaaaa aa aaaa aaa.aa"}, {"title", "Aaaaaaaaaaaa Aaaaaaa, aaa Aaaaaaa"}, {"title", "Aaaa Aaaaaaaa aaaa Aaaaa"}, {"title", "Aaaaaaa Aaaaaaa aaaa Aaaaa"}, {"title", "Aaaaa Aaaaaaa aa Aaaaaa"}, {"title", "Aaaaaaaaa Aaaaa Aaaa Aaaaaaa, Aaa Aaaaaaa"}, {"title", "Aaaaaaaaa Aaaaaaa Aaaaaa