functional-programming

Why does a Javascript function act differently upon instantiation than execution?

时光毁灭记忆、已成空白 提交于 2020-01-04 06:08:40
问题 I'm coming from C#/PHP and trying to get my head around Javascript's idea that functions are variables/objects and have quasi constructors, etc. Can anyone explain why the following code functions as it does, namely: Why isn't "2" displayed when instantiating the variable/function test ? Why isn't "1" displayed when executing the variable/function test ? code: var setup = function () { console.log(1); return function() { console.log(2); }; }; var test = setup(); // 1 test(); // 2 test(); // 2

Problem with list in Lisp

本小妞迷上赌 提交于 2020-01-04 06:04:43
问题 I am trying to write a simple procedure in Lisp to insert an element into binary search tree. I represented the tree as a list: the first element in the tree is the root the second element is the left sub-tree the third element is the right sub-tree This is my code: (define Insert (lambda (x T) (if (null? T) (list x '() '()) (if (> x (car T)) (list (car T) (cadr T) (Insert x (list (caddr T)))) (list (car T) (Insert x (cadr T)) (list (caddr T))))))) When I call the procedure like this: (Insert

Scheme/Racket filter/map multiple arguments

a 夏天 提交于 2020-01-04 05:08:31
问题 Lets say I want to do the following: (define (foo lst x) (filter function lst) but function takes in 2 arguments (and function was given to me), one being the list lst it will use, and the other being x . Syntactically, how would I change that line to pass in the second argument? Sorry I am new to Scheme/DrRacket. 回答1: Try this, using curry: (define (foo lst x) (filter (curry function x) lst)) That is, assuming that function takes as first parameter x and as second parameter each one of the

Higher order functions: automatic generation vs manual definition

喜夏-厌秋 提交于 2020-01-04 02:57:08
问题 I'm trying to delay evaluation for a bit, and so I prefer to work with functions as long as possible. I have class Function which defines composition and pointwise arithmetics for functions: from functools import reduce def compose(*funcs): ''' Compose a group of functions f1, f2, f3, ... into (f1(f2(f3(...)))) ''' result = reduce(lambda f, g: lambda *args, **kaargs: f(g(*args, **kaargs)), funcs)) return Function(result) class Function: ''' >>> f = Function(lambda x : x**2) >>> g = Function

What is the supertype of all functions in Scala?

ε祈祈猫儿з 提交于 2020-01-04 02:44:14
问题 I know I can do instanceOf checks against Function1 or Function2 etc but is there a generic way to see if something is function or not (it can have arbitray number of args). I tried defining something like this: type FuncType = (Any*) -> Any But that did not work either. Basically I have some code that looks like this: call = (name: Any, args: Any*) -> if name.isFunction then name.castAs[Function].apply(args) else name aFunction = (name: String) => "Hello " + name notAFunction = "Hello rick"

How to traverse array from both left to right and from right to left?

房东的猫 提交于 2020-01-04 01:57:29
问题 Suppose I have an imperative algorithm that keeps two indices left and right and moves them from left to right and from right to left var left = 0 var right = array.length - 1 while (left < right) { .... } // move left and right inside the loop Now I would like to write this algorithm without mutable indices. How can I do that ? Do you have any examples of such algorithms ? I would prefer a non-recursive approach. 回答1: You can map pairs of elements between your list and its reverse, then go

Which Functional programming language offers best support in Eclipse?

醉酒当歌 提交于 2020-01-03 18:16:20
问题 As an exercise my team is looking at learning functional programming. One of the factors to choose a language is its support in Eclipse. Any language with Eclipse plug-in is fine but what language offers the best free plug-in? Bonus question: the best online/book tutorial for this language. 回答1: I don't know if it is the best, but it is evolving and improving fast: Scala IDE (up to Scala2.8.1RC1 right now) As for the best online Scala book: Programming Scala (Creative Commons Attribution

What are the benefits of an immutable struct over a mutable one?

喜欢而已 提交于 2020-01-03 17:01:04
问题 I already know the benefit of immutability over mutability in being able to reason about code and introducing less bugs, especially in multithreaded code. In creating structs, though, I cannot see any benefit over creating a completely immutable struct over a mutable one. Let's have as an example of a struct that keeps some score: struct ScoreKeeper { var score: Int } In this structure I can change the value of score on an existing struct variable var scoreKeeper = ScoreKeeper(score: 0)

What is the meaning of `abstract` in the `interface definition`?

强颜欢笑 提交于 2020-01-03 16:46:52
问题 I quite don't understand how the interface thing works in OCaml. Let's see an example: About the 'a So what the meaning of 'a here? I mean I understand that when describing the functions, 'a means arbitrary type . Then what's its meaning here? Does it mean arbitrary set ? Also, Why put 'a in front of set ? abstract When explaining this example, Jason Hickey's Introduction to Objective Caml says: we need to define a polymorphic type of sets ’a set abstractly . That is, in the interface we will

Scala Monoid[Map[A,B]]

两盒软妹~` 提交于 2020-01-03 14:16:15
问题 I'm reading a book with the following: sealed trait Currency case object USD extends Currency ... other currency types case class Money(m: Map[Currency, BigDecimal]) { ... methods defined } The discussion goes on to recognize certain types of operations on Money as being Monoidal so we want to create a Monoid for Money . What comes next though are listings I can't parse properly. First is the definition of zeroMoney . This is done as follows: final val zeroMoney: Money = Money(Monoid[Map