functional-programming

What is the difference between Scala's case class and class?

百般思念 提交于 2019-12-17 01:20:34
问题 I searched in Google to find the differences between a case class and a class . Everyone mentions that when you want to do pattern matching on the class, use case class. Otherwise use classes and also mentioning some extra perks like equals and hash code overriding. But are these the only reasons why one should use a case class instead of class? I guess there should be some very important reason for this feature in Scala. What is the explanation or is there a resource to learn more about the

Idiomatic R code for partitioning a vector by an index and performing an operation on that partition

﹥>﹥吖頭↗ 提交于 2019-12-17 00:19:24
问题 I'm trying to find the idiomatic way in R to partition a numerical vector by some index vector, find the sum of all numbers in that partition and then divide each individual entry by that partition sum. In other words, if I start with this: df <- data.frame(x = c(1,2,3,4,5,6), index = c('a', 'a', 'b', 'b', 'c', 'c')) I want the output to create a vector (let's call it z): c(1/(1+2), 2/(1+2), 3/(3+4), 3/(3+4), 5/(5+6), 6/(5+6)) If I were doing this is SQL and could use window functions, I

Idiomatic R code for partitioning a vector by an index and performing an operation on that partition

若如初见. 提交于 2019-12-17 00:19:02
问题 I'm trying to find the idiomatic way in R to partition a numerical vector by some index vector, find the sum of all numbers in that partition and then divide each individual entry by that partition sum. In other words, if I start with this: df <- data.frame(x = c(1,2,3,4,5,6), index = c('a', 'a', 'b', 'b', 'c', 'c')) I want the output to create a vector (let's call it z): c(1/(1+2), 2/(1+2), 3/(3+4), 3/(3+4), 5/(5+6), 6/(5+6)) If I were doing this is SQL and could use window functions, I

How to use underscore.js as a template engine?

我怕爱的太早我们不能终老 提交于 2019-12-16 22:09:24
问题 I'm trying to learn about new usages of javascript as a serverside language and as a functional language. Few days ago I heard about node.js and express framework. Then I saw about underscore.js as a set of utility functions. I saw this question on stackoverflow . It says we can use underscore.js as a template engine. anybody know good tutorials about how to use underscore.js for templating, especially for biginners who have less experience with advanced javascript. Thanks 回答1: Everything you

Undefined function error in LISP

孤街醉人 提交于 2019-12-14 04:23:57
问题 I am working on a Lisp program that contains code to read in the dimensions of boxes and then sort them from shortest to longest lengths (and set each of these new lengths as new variables). When I attempt to load my file into the interpreter, I get the following error: *** - EVAL: undefined function NEW-D1 I am confused as to why I'd be getting this error because new-d1 isn't a function, it's a variable for the length of the shortest edge of a given box. Here's the code where new-d1 is first

@tailrec why does this method not compile with 'contains a recursive call not in tail position'?

蓝咒 提交于 2019-12-14 03:56:30
问题 @tailrec private def loop[V](key: String): V = { key match { case _ => loop(key) } } This method doesn't compile and complains that it 'contains a recursive call not in tail position'. Can someone explain to me what's going on? This error message doesn't make sense to me. 回答1: It compiles ok if the generic type is specified: import scala.annotation.tailrec @tailrec private def loop[V](key: String): V = { key match { case _ => loop[V](key) } } I think the error message is misleading in this

What is the philosophy behind “functional programming” when you actually want to create a side effect?

穿精又带淫゛_ 提交于 2019-12-14 03:46:27
问题 I know that pure functions are in the "functional programming" paradigm, you create a function that doesn't have any side effects and that for a input it always return the same output like: function (a,b) { return a + b; } This is a pure function because for a input I always return the same output and I didn't create any side effects. Ok I got that. But how can I make "pure functions", how can I stay in the "functional programming" paradigm when I actually want to create a side effect, such

How to setup and teardown functional test data in Geb grails

情到浓时终转凉″ 提交于 2019-12-14 03:45:42
问题 I have many working/passing functional geb/spock tests (each extending GebReportingSpec) that are testing a web application with test data all created from the BootStrap.groovy at the beginning of the functional test suite. I want to move the test data creation into startup() / teardown() methods within each Spec, well actually I wanted to have them inherit it from a base class but apparently the StepWise has issues with inheritance. So, at present each of my test spec classes look something

Chaining method calls with Either

﹥>﹥吖頭↗ 提交于 2019-12-14 03:42:05
问题 I'd like to know if it is possible to create some kind of "method call chain", with all methods returning the same Either[Error,Result]. What i'd like to do is: call all the methods successively, and when method returns a Left(Error), then stop the method calls and return the first Left found in the call chain. I've tryied some stuff, with fold, map, projections... but i'm new to Scala and don't find any elegant solution. I've tryed some thing like that: def createUserAndMandatoryCategories

ES6 proxy work-around for “TypeError: Cannot create proxy with a non-object as target”?

泄露秘密 提交于 2019-12-14 03:28:22
问题 My last question here: How to store data of a functional chain of Monoidal List? had many great responses and one of them suggested to implement some sort of type structure in JavaScript: const TYPE = Symbol(); const typeOf = t => x => x == null ? x : Object.assign(x, { [TYPE]: t }); const isType = t => x => x == null ? false : x[TYPE] === t; const Foo = x => typeOf(Foo)(x); console.log( isType(Foo)(1) // false , isType(Foo)([]) // false , isType(Foo)({}) // false , isType(Foo)(x => x) //