functional-programming

Java - fun converting a string (4 chars) to int and back

六月ゝ 毕业季﹏ 提交于 2021-01-28 03:10:06
问题 Please don't ask why but I have to store a string (max 4 char) in an integer value (so 4 bytes). First I wrote this and it works: String value = "AAA"; int sum = IntStream.range(0, value.length()) .limit(4) .map(i -> value.charAt(i) << (i * 8)) .sum(); System.out.println(sum); I was unable to think a functional solution for the way back. StringBuffer out = new StringBuffer(); while (sum > 0) { int ch = sum & 0xff; sum >>= 8; out.append((char) ch); } Any idea to write the way back (to "AAA")

How many different functions are there from Bool to Bool?

独自空忆成欢 提交于 2021-01-27 19:03:04
问题 Since this is (at least it seems to me) tightly related to programming, I'm asking here rather than on math or cs, but if you it think it best fits there or in another side, please just give your opinion. At the end of Chapter 2 of Bartosz Milewski's Category Theory for Programmers , there's this question: How many different functions are there from Bool to Bool ? Can you implement them all? This is my reasoning: Bool has only two elements in it, True and False ; different refers to what the

Is there a valid array monad transformer?

点点圈 提交于 2021-01-27 18:33:10
问题 I know how to implement the single linked list monad transformer but couldn't get its array counterpart running. The problem is that there is a grouping effect which renders the transformer only valid for commutative base monads. Here is an example where for the sake of simplicity both the transformer and the base monad are arrays and there is no transformer type wrapper: // ARRAY const arrMap = f => xs => xs.map((x, i) => f(x, i)); const arrAp = tf => xs => arrFold(acc => f => arrAppend(acc)

scala passing function with underscore produces a function not a value

柔情痞子 提交于 2021-01-27 13:54:16
问题 Hi I was writing any possible variations of passing a function to map, my initial understanding that they would all produce the same result, but I found that the lines 2, 3, actually produced different output, and line 4 is a mystery to me def g(v: Int) = List(v - 1, v, v + 1) val l = List(1, 2, 3, 4, 5) // map with some variations println(l.map { x => g(x) }) println(l.map { (_: Int) => g(_) }) // line 2 println(l.map { (_) => g(_) }) // line 3 println(l.map { _ => }) // line 4 println(l.map

Combining n vectors into one vector of n-tuples

南笙酒味 提交于 2021-01-27 11:44:35
问题 I'm thinking about a function with signature template<typename ...Ts> std::vector<std::tuple<Ts...>> join_vectors(std::vector<Ts>&&...) { //... }; but probably a more general one accepting any iterable instead of just std::vector would be good. Probably it would have a signature like this? template<template<typename> typename C, typename ...Ts> C<std::tuple<Ts...>> join_vectors(C<Ts>&&...) { // ... }; However, I'm not at this level yet in C++ (despite doing the same in Haskell would be

Is this JavaScript function, taking a mutable reference argument, a pure function?

五迷三道 提交于 2021-01-27 07:11:13
问题 I have the same question as this one, but in the context of JavaScript. From Wikipedia: [a pure function's] return value is the same for the same arguments It's further claimed there that a pure function is not allowed to have a variation in return value with "mutable reference arguments". In JavaScript, every normal object is passed as a "mutable reference argument". Consider the following example: const f = (arr) => arr.length const x = [] console.log( f(x) ) // 0 x.push(1); console.log( f

Is this JavaScript function, taking a mutable reference argument, a pure function?

不羁岁月 提交于 2021-01-27 07:09:50
问题 I have the same question as this one, but in the context of JavaScript. From Wikipedia: [a pure function's] return value is the same for the same arguments It's further claimed there that a pure function is not allowed to have a variation in return value with "mutable reference arguments". In JavaScript, every normal object is passed as a "mutable reference argument". Consider the following example: const f = (arr) => arr.length const x = [] console.log( f(x) ) // 0 x.push(1); console.log( f

Default parameter value undefined; is this a JavaScript Bug?

房东的猫 提交于 2021-01-27 06:46:25
问题 Below is a syntactically valid javascript program – only, it doesn't behave quite the way we're expecting. The title of the question should help your eyes zoom to The Problem Area const recur = (...args) => ({ type: recur, args }) const loop = f => { let acc = f () while (acc.type === recur) acc = f (...acc.args) return acc } const repeat = n => f => x => loop ((n = n, f = f, x = x) => // The Problem Area n === 0 ? x : recur (n - 1, f, f (x))) console.time ('loop/recur') console.log (repeat

Default parameter value undefined; is this a JavaScript Bug?

笑着哭i 提交于 2021-01-27 06:45:48
问题 Below is a syntactically valid javascript program – only, it doesn't behave quite the way we're expecting. The title of the question should help your eyes zoom to The Problem Area const recur = (...args) => ({ type: recur, args }) const loop = f => { let acc = f () while (acc.type === recur) acc = f (...acc.args) return acc } const repeat = n => f => x => loop ((n = n, f = f, x = x) => // The Problem Area n === 0 ? x : recur (n - 1, f, f (x))) console.time ('loop/recur') console.log (repeat

Methods to exhaustively partition a vector into pairs in R

大憨熊 提交于 2021-01-27 06:23:53
问题 (This is inspired by another question marked as a duplicate. I think it is an interesting problem though, although perhaps there is an easy solution from combinatorics, about which I am very ignorant.) Problem For a vector of length n , where n mod 2 is zero, find all possible ways to partition all elements of the vector into pairs, without replacement, where order does not matter. For example, for a vector c(1,2,3,4) : list(c(1,2), c(3,4)) list(c(1,3), c(2,4)) list(c(1,4), c(2,3)) My