functional-programming

What does sharing refer to in the implementation of a functional programming language

六月ゝ 毕业季﹏ 提交于 2019-12-22 07:35:30
问题 Sharing means that temporary data is stored if it is going to be used multiple times. That is, a function evaluates it's arguments only once. An example would be: let x = sin x in x * x What other features contribute to sharing and how would they interact with the need for practical programs to perform IO? 回答1: The clearest example of sharing in functional programming comes from Clean, which is based on graph rewriting. There, a computation refers to a DAG, so we can view the expression (sin

Javascript vs Python with respect to Python 'map()' function

纵然是瞬间 提交于 2019-12-22 07:04:12
问题 In Python there is a function called map that allows you to go: map(someFunction, [x,y,z]) and go on down that list applying the function. Is there a javascript equivalent to this function? I am just learning Python now, and although I have been told javascript is functional language, I can see that I have been programming in a non-functional javascript style. As a general rule, can javascript be utilized as a functional language as effectively as Python can? Does it have similar tricks like

String coverage optimization in Python

谁说胖子不能爱 提交于 2019-12-22 06:46:51
问题 I have this initial string. 'bananaappleorangestrawberryapplepear' And also have a tuple with strings: ('apple', 'plepe', 'leoran', 'lemon') I want a function so that from the initial string and the tuple with strings I obtain this: 'bananaxxxxxxxxxgestrawberryxxxxxxxar' I know how to do it imperatively by finding the word in the initial string for every word and then loop character by character in all initial string with replaced words. But it's not very efficient and ugly. I suspect there

Scala: Get sum of nth element from tuple array/RDD

一个人想着一个人 提交于 2019-12-22 06:41:28
问题 I have a array of tuple like this: val a = Array((1,2,3), (2,3,4)) I want to write a generic method for a method like below: def sum2nd(aa: Array[(Int, Int, Int)]) = { aa.map { a => a._2 }.sum } So what I am looking for a method like: def sumNth(aa: Array[(Int, Int, Int)], n: Int) 回答1: There are a few ways you can go about this. The simplest is to use productElement : def unsafeSumNth[P <: Product](xs: Seq[P], n: Int): Int = xs.map(_.productElement(n).asInstanceOf[Int]).sum And then (note

Javascript equivalents for Java Streams API

一世执手 提交于 2019-12-22 06:40:27
问题 I like the Java 8's streaming API. There are plenty of useful intermediate and terminal methods to transform and collect the stream. I'm talking about intermediate methods like distinct() or terminal methods like collect() . I find the Collector API especially useful, to reduce the stream to deep grouping maps. What is the javascript equivalent for the Java streaming API? I know there're basic functions like map , filter and reduce , but don't find any more generalized interfaces provided by

Avoid isPresent() and get() in control logic

强颜欢笑 提交于 2019-12-22 06:33:11
问题 Is there a prettier way of doing the following in Java 8, avoiding isPresent and get ? void doStuff(String someValue, Optional<Boolean> doIt) { if (doIt.isPresent()) { if (doIt.get()) { trueMethod(someValue); } else { falseMethod(someValue); } } } I tried using map , without success. But I probably didn't try hard enough? 回答1: You can use ifPresent instead of isPresent and get : void doStuff(String someValue, Optional<Boolean> doIt) { doIt.ifPresent (b -> { if (b) trueMethod(someValue); else

Playing with infinity - Lazy arithmetics

旧巷老猫 提交于 2019-12-22 06:10:37
问题 Many modern programming languages allow us to handle potentially infinite lists and to perform certain operations on them. Example [Python]: EvenSquareNumbers = ( x * x for x in naturals() if x mod 2 == 0 ) Such lists can exist because only elements that are actually required are computed. (Lazy evaluation) I just wondered out of interest whether it's possible (or even practised in certain languages) to extend the mechanism of lazy evaluation to arithmetics. Example: Given the infinite list

Playing with infinity - Lazy arithmetics

本秂侑毒 提交于 2019-12-22 06:10:02
问题 Many modern programming languages allow us to handle potentially infinite lists and to perform certain operations on them. Example [Python]: EvenSquareNumbers = ( x * x for x in naturals() if x mod 2 == 0 ) Such lists can exist because only elements that are actually required are computed. (Lazy evaluation) I just wondered out of interest whether it's possible (or even practised in certain languages) to extend the mechanism of lazy evaluation to arithmetics. Example: Given the infinite list

How to use Ramda to find matching object in Array by key value

人走茶凉 提交于 2019-12-22 06:09:10
问题 Ramda REPL example var portfolio = [{ticker: "aa"}, {ticker: "bb"}]; var ticker = {ticker:"aa"}; var exist = R.find(R.propEq('ticker', ticker), portfolio) console.log(exist) Currently this is giving me undefined , however R.propEq should find the matching object by key ticker in port I thought? 回答1: As you say, you can solve it by passing in the key to propEq: R.find(R.propEq('ticker', 'aa'), port) Another option is to use the eqProps function, which tests if two objects match for the named

Why function composition sometimes requires two “.” 's to combine two functions

霸气de小男生 提交于 2019-12-22 05:59:30
问题 So this question is simple but I can't seem to grasp the concept. To compose ordinary functions, one can just do something like below: lowerNoSpaces = filter (/= ' ') . map toLower But, on occasion, there are times where this won't work: myConcatMap = concat . map It gives the error: <interactive>:236:1: error: * Non type-variable argument in the constraint: Foldable ((->) [a1]) (Use FlexibleContexts to permit this) * When checking the inferred type concattMap :: forall a1 a2. Foldable ((->)