0
$\begingroup$

I am very new to functional programming so sorry if the question is stupid.

Having this function

const noname = func => val => bool => bool? func(val) : val

And following situation

// if event.target has css class 'draggable-box' , add drag listener to it, using event
compose(
    noname( add_drag_listener )(event),
    elem_has_css_class('draggable-box'),
    prop('target'),
)(event) 

In Category theory, what is the correct name for the noname function? Is there such a type?

$\endgroup$
0

2 Answers 2

1
$\begingroup$

I don't believe that there is a standard category theory name for this, because of the Boolean argument. So what this function means would depend on how that is constructed.

In what follows, I'm going to change the argument order by putting that Boolean first.

If you encode the Boolean using Church encoding, the function would look like this in lambda calculus:

$$\lambda b. \lambda f. \lambda x. b\,(f\,x)\,x$$

This doesn't look especially standard to me. In Haskell, the function is called applyWhen.

Interpreting the Boolean as a subobject classifier in an elementary topos is left as an exercise.

$\endgroup$
0
$\begingroup$

Maybe currying is the syntax you refer, and passing arguments as functions:

const sum = (a, b, c) => a + b + c;
const partial = a => b => c => sum(a, b, c);

console.log(partial(1)(2)(3));          // 6

$\endgroup$
2
  • $\begingroup$ thanks , but i know currying .it's already in use . all the functions in my code example are curried . $\endgroup$ Mar 12, 2021 at 2:36
  • 1
    $\begingroup$ The code OP gives has almost nothing to do with currying other than it's an implementation detail. The question is how is this type of function called and it appears to be a combinator - a type of higher order function that is generic but works for specific purpose. Some have well known names, like the join combinator: f => x => f(x)(x) or flip combinator: f => x => y => f(y)(x). Currying is more of an implementation detail. flip can also be defined as f => (x, y) => f(y, x) or f => (...args) => f(args.reverse()). $\endgroup$
    – VLAZ
    Mar 15, 2021 at 9:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.