purely-functional

Scala String Equality Question from Programming Interview

我怕爱的太早我们不能终老 提交于 2020-01-01 04:02:10
问题 Since I liked programming in Scala, for my Google interview, I asked them to give me a Scala / functional programming style question. The Scala functional style question that I got was as follows: You have two strings consisting of alphabetic characters as well as a special character representing the backspace symbol. Let's call this backspace character '/'. When you get to the keyboard, you type this sequence of characters, including the backspace/delete character. The solution you are to

Functional way to get previous element during map

情到浓时终转凉″ 提交于 2019-12-24 11:19:40
问题 I have an array which I map over. I need to compare the current element with the previous. I am detecting if the current element is the same as the previous element by comparing their id s and doing something different based on this condition. Is there any purely functional way to do it without doing index math? items.map((item, index) => { if(item.id === items[index - 1 > 0 ? index - 1 : 0].id) { // do something } else { // do something else } }) The code works but I would like to avoid

Understanding the evaluation and execution order with semicolon syntax

感情迁移 提交于 2019-12-23 06:28:45
问题 I already learned a little bit about FFL semicolons from my previous question. However, it is still not clear what order of evaluation or execution they enforce. So here is a more concrete example: [ expr_a, expr_b ; expr_c, expr_d ; expr_e, expr_f ] What should be the order of execution for the above code? In my head, it should be: evaluate a & b execute a, execute b evaluate c & d execute c, execute d evaluate e & f execute e, execute f Now let's imagine that expr_b = add(test_list, ['b saw

Analysis and Design for Functional Programming [closed]

房东的猫 提交于 2019-12-22 04:14:23
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . How do you deal with analysis and design phases when you plan to develop a system using a functional programming language like Haskell

How to perform side-effects in pure functional programming?

喜你入骨 提交于 2019-12-20 12:34:35
问题 I am dealing with the concept of functional programming for a while now and find it quite interesting, fascinating and exciting. Especially the idea of pure functions is awesome, in various terms. But there is one thing I do not get: How to deal with side-effects when restricting yourself to pure functions. E.g., if I want to calculate the sum of two numbers, I can write a pure function (in JavaScript): var add = function (first, second) { return first + second; }; No problem at all. But what

Why are “pure” functions called “pure”? [closed]

早过忘川 提交于 2019-12-17 18:45:46
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 years ago . A pure function is one that has no side effects -- it cannot do any kind of I/O and it cannot modify the state of anything -- and it is referentially transparent -- when called multiple times with the same inputs, it always gives the same outputs. Why is the word "pure" used to describe functions with those

Timing out pure functions

China☆狼群 提交于 2019-12-12 08:19:03
问题 How can I "kill" a pure calculation which is taking too long? I tried import System.Timeout fact 0 = 1 fact n = n * (fact $ n - 1) main = do maybeNum <- timeout (10 ^ 7) $ (return . fact) 99999999 print maybeNum However, this doesn't work. Replace the (return . fact) 99999999 with a "real" IO function like getLine and this works as expected. 回答1: The point is that return (fact 999999999) immediately returns and doesn't trigger the timeout. It returns a thunk that will be evaluated later. If

Are side effects everything that cannot be found in a pure function?

て烟熏妆下的殇ゞ 提交于 2019-12-12 07:31:40
问题 Is it safe to say that the following dichotomy holds: Each given function is either pure or has side effects If so, side effects (of a function) are anything that can't be found in a pure function. 回答1: This very much depends on the definitions that you choose. It is definitely fair to say that a function is pure or impure . A pure function always returns the same result and does not modify the environment. An impure function can return different results when it is executed repeatedly (which

Splitting complex String Pattern (without regex) in a functional approach

爷,独闯天下 提交于 2019-12-11 09:54:33
问题 I am trying to split a string without regex in a more idiomatic functional approach. case class Parsed(blocks: Vector[String], block: String, depth: Int) def processChar(parsed: Parsed, c: Char): Parsed = { import parsed._ c match { case '|' if depth == 0 => parsed.copy(block = "", blocks = blocks :+ block , depth = depth) case '[' => parsed.copy(block = block + c, depth = depth + 1) case ']' if depth == 1 => parsed.copy( block = "", blocks = blocks :+ (block + c), depth = depth - 1) case ']'

Updating immutable objects

守給你的承諾、 提交于 2019-12-11 02:45:21
问题 I have the following class built: class Player(val name: String, val onField: Boolean, val draft: Int, val perc: Int, val height: Int, val timePlayed: Int) { override def toString: String = name } I'm trying to do def play(team: List[Player]): List[Player] = team map (p => new Player(p.name, p.onField, p.draft, p.perc, p.height, p.timePlayed + 1)) which is actually incrementing the field "timePlayed" by one, and return the new "List" of players. Is there a more convenient way to do this?