259 questions
4
votes
0
answers
74
views
Why does Haskell's type inference give these functions different type signatures? [duplicate]
I am a professor teaching Haskell in a programming languages class. We are covering partial application and currying. So, I gave the following example functions:
multThenAdd x y z = (x * y) + z
...
0
votes
0
answers
47
views
Python3 Type Safe partial_apply
I'm trying to implement a helper function which partial apply arguments to a specific function and return a new function
from typing import Any, Callable, TypeVar
from typing_extensions import ...
3
votes
0
answers
65
views
For function definition, what is the difference between using partial application, a lamba expression or put the arguments next to the name? [duplicate]
What are the differences between the three syntaxes
funName l = map fun l
funName = \ l -> map fun l
funName = map fun
In general it does the same thing however in some cases it differs.
The ...
1
vote
1
answer
84
views
How to write a precise type signature for (an equivalent of) Function.prototype.bind?
I'm trying to create a generic bind function for TypeScript that will apply a function with any number of parameters to a specific this value.
Without types, the idea is simple:
function bind(self, fn)...
1
vote
0
answers
72
views
Replacing single elements in list
So, I am currently doing an exercise where I have a list of employees, with the data type Employee.
data Employee = Employee Name Age Salary deriving (Show)
employees :: [Employee]
employees =
[ ...
0
votes
1
answer
65
views
How to filter an array using partical function application
I am working with a program that creates an array of data. The user should be able to use all entries or apply a filter. The filter function actually contains a lot of variables not directly related ...
0
votes
1
answer
113
views
F# Is it possible to get arguments of partially applied function?
Having a function taking two arguments:
let yolo x y =
x + y
Is it possible to get information (or preferably, value) of one of the applied arguments after an application?
Below is a pseudo-code ...
0
votes
5
answers
244
views
js function repeatedly invokes through an array of multipliers
I am running into an error: Uncaught TypeError TypeError: multipliers.reduce(...) is not a function. I was looking at currying and partial application. I expected multiply function to correctly invoke ...
0
votes
0
answers
88
views
Using partially applied functions in Scala [duplicate]
I am a newbie in Scala and am trying to understand partially applied functions. I can implement simple examples like when function takes two integers as input. But I wanted to combine variable number ...
0
votes
1
answer
116
views
Partial function application in Scala for arbitrary input arguments
I am wondering whether the following is possible in Scala:
Given some vector x = (x_1, x_2, ..., x_n) in R^n and a function f that maps R^n to R, I would like to replicate this concept in Scala. The ...
1
vote
1
answer
503
views
I need to provide a callback when instantiating a class from an API. How can I bind the instance itself to the callback, eagerly?
I've encountered this problem with a few different major third-party libraries and frameworks now. Let me try to boil it down to the essentials:
The API provides a class Example, where the ...
6
votes
3
answers
128
views
Curried function defined in terms of its own partial application
The following SML code is taken from a homework assignment from a course at University of Washington. (Specifically it is part of the code provided so that students could use it to complete Homework 3 ...
0
votes
1
answer
904
views
How to inject parameters into return function with custom hook react
Usually when we use <input /> field in html, we get a callback function such as <input onChange = {(event) => console.log(event.target.value)} How is this event argument passed back to us?
...
0
votes
1
answer
706
views
Why does partial application work when currying but not with .bind()?
I'm practicing partial application of a function, that is, fixing function arguments. I've learned two ways to achieve it:
By currying the original function first.
By using .bind() method.
In the ...
2
votes
2
answers
154
views
Reversing partial function parameters sequence
I want to write
List.map (fun x -> x % 3) into a form like List.map ((%) 3).
The issue with the latter is that it translates to
List.map (fun x -> 3 % x) Not what I want.
Is it possible to write ...
0
votes
0
answers
30
views
Do Python's `lambda` remember references to outer variable rather value? [duplicate]
I was expecting Python's lambda to keep reference to value of local variables. So when I wrote:
>>> ls = [
... lambda: x*x
... for x in range(3)
... ]
>>> [f() for f in ls]
[...
0
votes
1
answer
121
views
What's different between these 2 partial function objects?
Precondition
I have these definitions:
def add(x,y): return (lambda x,y: x+y)(x,y)
def call(f,x,y): return f(x,y)
and these import aliases:
from multiprocessing import Pool as P;
from functools ...
1
vote
1
answer
144
views
Passing _.groupBy to _.partialRight seems to give incorrect results
Here's a JavaScript object,
const obj = {a: [{ id: 1 }, {id: 1}, {id: 2}, {id: 3}], b: [{ id: 4 }, {id: 5}, {id: 5}, {id: 6}] };
and here's a code that correctly groups the items by .id in each of ...
4
votes
1
answer
2k
views
How does compose function work with multiple parameters?
Here's a 'compose' function which I need to improve:
const compose = (fns) => (...args) => fns.reduceRight((args, fn) => [fn(...args)], args)[0];
Here's a practical implementation of one:
...
3
votes
1
answer
98
views
Why does ($ 3) have signuature (a -> b) -> b?
In Learn you a Haskell, it is given the following example:
map ($ 3) [(4+), (10*), (^2), sqrt]
[7.0,30.0,9.0,1.7320508075688772]
However, I don't understand why this works.
The signatures of the ...
1
vote
2
answers
216
views
F# partial application function wrapper does not keep return type generic for parameter function
TL DR: I am trying to create a function wrapper. The wrapped function takes no parameter but returns a value.
The reason I want to do this is to create a function similar to "lock" but for a ...
0
votes
3
answers
107
views
Why does this method of detecting keypresses with the Turtle module in Python not work? [duplicate]
Having read...
How can I log key presses using turtle?
I am trying to detect key presses using a slightly different method.
Here is a simplified version of my code, which works as expected...
from ...
0
votes
1
answer
366
views
Typescript: Type a group of partially applied functions with variable number and type of parameters that all return function of same type
I am looking for a way to group a set of partially applied functions with only the first parameter(s) being different. Or in other words; The group contains functions accepting any number of ...
3
votes
2
answers
104
views
About mapping on the eventual result of a multi-argument function
I known that r -> a is a Functor in a, and that fmap = (.) for it.
This means that when I do fmap f g, with g :: r -> a, f is applied to the result of g as soon as the latter is fed with a value ...
1
vote
1
answer
230
views
Partially applying function arguments in a composition
I'm trying to write a function composition that partially applies an argument at each step and ends up calling a curried two-argument function.
There is a set of example functions to compose. I ...
1
vote
2
answers
96
views
Why does `let f : int -> int list -> int list = (::);;` fail?
I would think that OCaml would read the following as an instance of partial application, and store the function as f. However, the compiler complains that the function is applied to too few arguments....
0
votes
0
answers
123
views
Partial application of map function
Just defined a generic map function in Swift:
func map<T, S: Sequence>(_ f: @escaping (S.Element) -> T) -> (S) -> [T] {
return { xs in xs.map(f) }
}
However, when I try to ...
3
votes
1
answer
170
views
Zero or all optional but typed parameters
I am just about to learn some typed functional programming, so just started with an implementation of partial application - which should be type safe.
Problem: I am trying to make a function that ...
1
vote
2
answers
481
views
changing list of strings to atoms using maplist?
How to change string to atoms using maplist.
This does not work :
?- maplist(atom_string,["a","b","c"]).
first because atom_string/2 has arity of two (How do you do ...
3
votes
1
answer
718
views
No instance for (Num (Int -> Int)) arising from a use of syntactic negation
I have written the following Haskell code to return the primary and secondary diagonal of [[Int]]
getDiagonal' :: [[Int]] -> Int -> (Int -> Int) -> [Int]
getDiagonal' [] _ _ = []
...
1
vote
1
answer
73
views
Using point free style for parts of a function definition [duplicate]
Given this function (btw, should I say it's defined by cases? How do I refer to functions defined like this?),
f :: Int -> Int -> Int
f 0 x = x
f x _ = x
I'm wandering what is the reason, if ...
0
votes
1
answer
221
views
Using partial() on DRF's @action gives TypeError: "multiple values for argument"
I need similar @action detail routes on multiple ViewSets. I want to make sure they remain consistent. I don't want to put @action(methods=["post"], detail=True, url_path="something&...
0
votes
1
answer
520
views
Partial Application Macro in Rust, Works But
So I was writing (attempting) some variadic macros to try to implement compose and curry in rust. pipe was easy enough.
Variadic partial application though? Not so much. Props to anyone who can come ...
1
vote
1
answer
1k
views
How to type a partially applied function in typescript?
How can I properly type the return type of the following function without using any? It's a function that, depending on the presence of one parameter, returns a string or a function.
function ...
1
vote
1
answer
197
views
F# - Partial application of a Int32.TryParse
I was trying to do this
let TryParseAnyNumberStyle = Int32.TryParse(style= NumberStyles.Any, privider=CultureInfo.CurrentCulture)
But the compiler complains
The member or object constructor '...
2
votes
2
answers
595
views
How to dynamically add method to class with `functools.partial()`
I am having trouble with the right incantation to get a dynamic method added to a class using functools.partial in the following situation. The following has a Creator class to which I want to add a ...
2
votes
1
answer
116
views
Typo in Learn you a Haskell for Great Good? [closed]
In Type Synonyms we read
Just like we can partially apply functions to get new functions, we can partially apply type parameters and get new type constructors from them.
How can a parameter be ...
0
votes
1
answer
101
views
Varargs function argument of generic partial application does not type check
I have the following combinator that converts a mutli-argument function in one that can be partially applied:
type Tuple = any[];
const partial = <A extends Tuple, B extends Tuple, C>
(f: (.....
0
votes
1
answer
136
views
Refactor functions to use currying
I have three functions that I would like to refactor using currying but I am struggling get my head around functions that return functions. I am trying to break down and design the curried functions ...
0
votes
1
answer
49
views
Is it possible for a partially applied function to call its partially applied self?
I've started playing with Akka and have found that most of my actors have part immutable state and part mutable state. Both could be merged into a State case class which could then be copied on solely ...
2
votes
1
answer
243
views
Is partial application of higher order functions possible in C? [duplicate]
Let's say I have a function
int myfun (int arg1, int arg2, int arg3, int arg4) {
/* function body */
}
and I would like to write a function pass_last_elements() that has signature
int (*)(int, ...
7
votes
1
answer
190
views
Partial application versus pattern matching: why do these Haskell functions behave differently?
I'm trying to understand something about Haskell functions.
First, here is a Fibonacci function defined in the typical "slow" way (i.e. recursive with no memoization, and no infinite-list tricks)
...
3
votes
1
answer
101
views
Partial functions keeping their signature
We can use purrr::partial to create partial functions:
f <- function(x, y) {
print(x)
print(y)
return(invisible())
}
ff <- purrr::partial(f, y = 1)
ff(2)
#> [1] 2
#> [1] 1
Created ...
2
votes
3
answers
124
views
How can I avoid accidental partial application in a compact way?
I have a side-effecting function, f : int -> string -> unit which I am calling using f 1 "hi".
To make sure I get an error in the call site if the function is changed to need more arguments, I ...
3
votes
2
answers
340
views
Dynamically adding builtin methods to point to a property's built-ins [duplicate]
I have a couple classes and a function:
from functools import partial
def fn(other, self, name):
print(f"calling {name} with {other}")
func = getattr(self.a, name)
return func(other)
...
3
votes
1
answer
160
views
kind-projector returns strange results
I have these types:
SomeTypeClass
A higher kinded type which has one type parameter of kind * => * => *
trait SomeTypeClass[P[_, _]] {
def test[F[_], S, T, A, B](f: (A => F[B]) => S => ...
0
votes
1
answer
194
views
reasonml type higher order function
given the following module the compiler raises an error
41 │ };
42 │
43 │ module TestB = {
44 │ let minFn = (a, b) => a < b ? a : b;
. │ ...
54 │ let max = reduceList(maxFn);
...
1
vote
0
answers
102
views
Can I rewrite this code to avoid the F# error
I have the following code in F#
live version at https://repl.it/repls/CarefulGiganticExtraction
let inline tryParse text =
let mutable r = Unchecked.defaultof<_>
(^a : (static member ...
73
votes
3
answers
13k
views
What exactly is meant by "partial function" in functional programming?
According to my understanding, partial functions are functions that we get by passing fewer parameters to a function than expected. For example, if this were directly valid in Python:
>>> ...
5
votes
2
answers
1k
views
Performant way to partially apply in Python?
I am looking for a way to partially apply functions in python that is simple to understand, readable, resusable and as little error prone to coder mistakes as possible. Most of all I want the style to ...