Questions tagged [function-composition]

Applying one function to the result of another is known as function composition: `(f.g)(x) = f(g(x))`

Applying one function to the result of another is known as function composition: (f.g)(x) = f(g(x)).

See also .

420 questions
0
votes
2 answers

"Adding" two functions together in Scheme

I am going through a practice exam for my programming languages course. One of the problems states: Define a function named function+ that “adds” two functions together and returns this composition. For example: ((function+ cube double) 3) should…
Ashley
0
votes
1 answer

Compiler error when using function composition: Cannot convert value of type `([Character]) -> String` to expected > argument type `_ -> _`

I am playing around with the custom operator >>> for function composition that is suggested here. I have defined the following: infix operator >>> { associativity left } func >>> (f: B -> C, g: A -> B) -> (A -> C) { return { x in…
nburk
  • 22,409
  • 18
  • 87
  • 132
0
votes
1 answer

Carrying state/IO through a chain of functions in Clojure to be used later in the chain

I've got a bit of a functional question. If I have a functional chain where I compose functions in a way that the output of the previous is the input of the next one. And somewhere down that chain, I need stuff to do IO, like a client to a DB,…
Didier A.
  • 4,609
  • 2
  • 43
  • 45
0
votes
3 answers

Why does feeding init to last not work in Haskell?

So I'm writing a line to get the second to last element of a list. Initially my code was mySLast x = last.take ((length x) - 1) x Which worked up until the last function. Realized the my take business is already included in Haskell as init so I…
Tshimanga
  • 845
  • 6
  • 16
0
votes
1 answer

Composing two functions in R gives different result than a new function enclosing both

I tried to look at the logistic map logMap <- function(x){ r <- 0.5 4*r*x*(1-x)} and its compositions such as: logMap2_a <- function(x){ logMap(logMap(x))} Of course, you can also directly write a new function that composes logMap with…
networker
  • 117
  • 1
  • 8
0
votes
1 answer

Problems wirh function composition in Haskell

So... I have written this to get 5 rectangles from user : rectangles <- sequence . take 5 . cycle [getRect] But it doesn't work. Then I have refactored it as this which is shorthand for writing braces: rectangles <- sequence $ take 5 $ cycle…
Reygoch
  • 1,204
  • 1
  • 11
  • 24
0
votes
2 answers

How can I define a function that applies the composition of three functions to a value?

I've got the following question, and I have no idea where to start: Write a function trifecta :: (a -> b) -> (b -> c) -> (c -> d) -> a -> d that takes 3 functions and composes them into a function that takes a value and produces a value. When…
Amr Hamada
  • 63
  • 1
  • 2
  • 8
0
votes
2 answers

Applying properties of a base type to a subtype in JavaScript

I would like to include the properties and initialization logic from a "base" type into a "sub" type in JavaScript. Is the following idiomatic? function Base(arg1) { this.foo = arg1.foo; } function Sub(arg1) { //Initialize using the base…
Ben Aston
  • 53,718
  • 65
  • 205
  • 331
0
votes
1 answer

How do I stay DRY with Meteor template helpers function composition?

I want to remain DRY with my templates so I created some amazing composable functions and helpers only to realize that Spacebars doesn't play nicely. I have 2 functions that both take two arguments: func(a,b) which in Spacebars turns into {{func a…
Chet
  • 18,421
  • 15
  • 69
  • 113
0
votes
1 answer

Implementing revmap function with pattern matching or function composition

Higher order function map Definition: map :: (a -> b) -> [a] -> [b] map f [] = [] map f (x:xs) = f x:map f xs Then how to write a revmap function with pattern matching/ using function composition?
0
votes
1 answer

Haskell: Partially applied function, function composition in a raytracer program

I have a problem understanding the function composition and the concept of partially applied functions. Actually I'm writing a small raytracer and have some example implemenentations which I dont understand exactly. Here is one example which I dont…
0
votes
0 answers

How to write general integration function in OCaml

I would like to write a function in OCaml that will calculate the definite integral for the given function. The problem is that I aim for the following syntax: let sphere r phi theta = r *. sin phi *. cos theta in let dphi = 10 in (* number of parts…
Bartek Chaber
  • 236
  • 1
  • 3
  • 7
0
votes
2 answers

How can I make type inference work with partial functions

I would like to define a class hierarchy of classes that handle messages using a partial function. Each class should defer messages that it can not handle to its superclass. Something like this: type Respond = PartialFunction[String,…
Rüdiger Klaehn
  • 12,445
  • 3
  • 41
  • 57
0
votes
1 answer

prevent scala function composition from inferring ? type

I'm trying to compose some functions with compound types in their type parameters: trait Contains[T] trait Sentence trait Token def sentenceSegmenter[T] = (c: Contains[T]) => null: Contains[T with Sentence] def tokenizer[T <: Sentence] = (c:…
Steve
  • 3,038
  • 2
  • 27
  • 46
0
votes
1 answer

How to change the wrapped action call during action composition in play framework 2.0

From what I understand of Action composition, we can wrap multiple action classes around an action class so that the wrapped classes are called first before the action action class so, for eg: @With(a.class, b.class) public static Result index() { …
Tapan Nallan
  • 1,762
  • 3
  • 17
  • 37
1 2 3
27
28