functional-programming

Haskell: moving image on horizontal line

不问归期 提交于 2019-12-13 10:13:20
问题 Im new to Haskell and am working with images represented as type Img = [String] . I want to move the image either left or right by 1 or more rows with a wrap. i've manged to move the images up or down, code below: moveVer :: Int -> Img -> Img moveVer n xs = take len $ drop (mod n len) $ cycle xs where len = length xs img 1 = XXXXXX OUTPUT = (moveVer (3)(img 1)) = XX XX XX XX XXXXXX XX XXXXXX XXXXXX XX Now i'm trying to do the same thing but move the image horizontally (left or right).

SML functional programming higher order function?

与世无争的帅哥 提交于 2019-12-13 09:49:49
问题 I need to implement a function ziprev : 'a list -> 'b list -> ('a * 'b) list - ziprev [1,2,3,4] [10,20,30,40]; val it = [(1,40),(2,30),(3,20),(4,10)] : (int * int) list Using a function that I already created: - zipW (fn (x, y) => x + y) [1,2,3,4] [10,20,30,40]; val it = [11,22,33,44] : int list and the List.rev from the library. I have no idea how to do a function with two libraries. Any suggestions? 回答1: Hint 1: Compare the result of your ziprev with List.zip [1,2,3,4] [10,20,30,40] You

How to implement Fibonacci number using recursion in JavaScript [duplicate]

南楼画角 提交于 2019-12-13 09:39:35
问题 This question already has answers here : JavaScript Fibonacci breakdown (3 answers) Closed 8 months ago . I tried playing around with recursion in javascript. I wrote a function for the Fibonacci sequence. However, if only works if the argument is 0 fib = (x)=>{ if (x == 0 || x ==1) { return 1 } else { return fib(x-1) + fib(x+1) } } It returns 1 for 1, but a number above 0 I get the error maximum call stack size exceeded 回答1: This is not the Fibonacci sequence. This is the Fibonacci sequence:

Implicit cast of functional interface

情到浓时终转凉″ 提交于 2019-12-13 09:16:36
问题 I want to implicitly cast my own interface implementation to a Java8 function. My code: import java.util.stream.Stream; @FunctionalInterface interface StringChanger { String change(String o); } public class A { public static void main(String[] args) { Stream.of("hello", "world") .map(new StringChanger() { @Override public String change(String o) { return o.trim(); } }) .forEach(System.out::println); } } Why does the cast not work? I'm getting this exception: Exception in thread "main" java

Return function that modifies the value of the input function

冷暖自知 提交于 2019-12-13 09:00:36
问题 How can I make a function that is given a function as input and returns a function with the value tripled. Here is some pseudo code for what I'm looking for. Concrete examples in Python or Scala would be appreciated. def f(int x): return x ** 2 def tipleFunc(Function g) return 3 * g Function newFunc = tripleFunc(f) print newFunc(5) 回答1: def f(x): return x ** 2 def tripleFunc(g): return lambda *args: 3 * g(*args) newFunc = tripleFunc(f) print newFunc(5) 回答2: In Scala: def foo(x: Int) = x * x

How do I return a component with passed props with R.ifElse?

梦想与她 提交于 2019-12-13 08:41:18
问题 I'm diving into ramdajs and refactoring some react code here. It might not necessary to do it in this case but for the sake of exercise how do I pass props in R.ifElse ? {!this.state.repos ? <Loader /> : <RepoGrid repos={this.state.repos} />} // refactoring to: {R.ifElse( R.isEmpty, R.always(Loader), RepoGrid )(this.state.repos)} This gives me an error Cannot read property '@@transducer/step' of null const ReposGrid = R.pipe( R.tap(console.log) R.prop("repos"), R.map(Repo), ReposWraper ) 回答1:

OCaml - return a list containing all the elements in even position in the input list

狂风中的少年 提交于 2019-12-13 08:19:31
问题 I am new to OCaml, and I am now trying to implement a function that returns a list containing all the elements in even position in the input list. For [1;2;3;5] returns [2;5] and for [1 3] returns [3]; and for ["i";"am";"new";"to";"ocaml"] returns ["am";"to"]. Hope someone can give me some suggestions to solve this problem. 回答1: There's nothing particularly OCaml-like in this problem. Most likely your main task is to learn to solve problems recursively. The way to solve a problem recursively

Import side effects

风格不统一 提交于 2019-12-13 08:18:05
问题 I am new to Go and functional paradigm. while working with database connections in golang I have to import mysql driver. And I came across "_" which is to mention blank identifier for variables and import packages which are soley for their side effects. Searched for side-effects and found this side effects in es6 What I didn't understand is side effect of a function is dependent on a global variable which deviates the referential transparency of a pure function. But how does a package can

How to Array.map chain off of async await?

为君一笑 提交于 2019-12-13 07:57:38
问题 How can I run Array.map off of an await? const CLASS_PATH = 'User/matt/Github/project'; const PACKAGE_JSON = 'package.json'; const walk = async path => { let dirs = []; for (const file of await readdir(path)) { if ((await stat(join(path, file))).isDirectory()) { dirs = [ ...dirs, file, ]; } } return dirs; }; async function main() { const packagePaths = await walk(CLASS_PATH) .map(pkgName => join(CLASS_PATH, pkgName, PACKAGE_JSON)); } main(); 回答1: Brackets can always be used to change operator

What is the point of this monadic law?

大憨熊 提交于 2019-12-13 04:49:33
问题 I am reading an article about "monadic laws". The first law the article mentions is: m map f ≡ m flatMap {x => unit(f(x))} For Scala Option it means: option map f ≡ option flatMap {x => Option(f(x))} Now I wonder what the law point is. Why is the law important ? What if Scala Option does not obey this law ? 回答1: If it does not obey the monad laws it's not a monad. That's actually why the unit of Option is Some.apply and not Option.apply . Just look at this case: scala> val f = (x: Int) =>