21
-red
3y

I’m writing funky code. I hope no one notices.

For instance, I needed to add a function which returns whatever is input to it.

Comments
  • 5
    Why the fuck?
  • 8
    It's called an identity function and it's kinda useful in FP
  • 3
  • 1
    Kinda sounds similar to an identity matrix.
  • 11
    @Root apart from being theoretically very (very) important, practically it's useful for eg. when you have an algorithm that takes functions as arguments and you don't want to to use some feature so you pass in an identity function to "do nothing".

    Eg. suppose you have a function
    contructEnumerated that takes an int "n" and a function "func" that takes an int as input and gives an int as output.

    constructEnumerated constructs a list of length "n" by applying "func" to the series 1...n, i.e something like
    contructEnumerated(n, func) = [func(1), func(2), func(3), func(4), func(5), ..., func(n)]

    And you want to use it to construct [1, 2, 3, 4]

    you would write that as constructEnumerated(4, id) where "id" is the identity function on ints, because that expands to [id(1), id(2), id(3), id(4)]

    A bit of a simple, contrived example, but it really is very useful.

    @Demolishun it's the same idea. The identity of an operator is the object that gives you back the same thing you put in. The identity of multiplication is 1, because 1* x = x. The identity of matrix multiplication is the identity matrix I, because matmul(I, M) = M. The identity of function application is the (parametric) identity function id, because apply(id, x) = x, where apply(f, x) = f(x). Identity function also shows up a fair bit in type theory etc.
  • 0
    Does it return something else with the input or does it modify data and then return so something knows what item it was acting on ?
  • 0
    @RememberMe that does not sound useful
  • 0
    def what(in_the)
    fuck = in_the
    return fuck
    end
  • 4
    The id function is the most efficient and most readable way to handle no-op corner cases when using higher-order functions.

    There is no reason to be ashamed of using it.

    Other devs who liked id also liked:
    never: Always returns false.
    always: Always returns true.
    none: Always returns null.
    all(guards): Returns a function of a returning true when for every guard in guards guard(a) is true.
    any(guards): Returns a function of a returning true when for any guard in guards guard(a) is true.
  • 0
    Ever since I started using the dbg! macro in rust, I tend to emulate it in python or js whenever I write in these. This function is an identity function with the side effect of printing whatever was passed to it (in rust it will also print the filename, line, and code of the printed expression).
    So in python, it's just:
    def dbg(x):
    print(x)
    return x
    A kinda stupid, but very convenient function
  • 0
    @RememberMe There is no need for it to be a seperate function, atleast in JS, example you could do this Arr.map((x) => x) it's an anonymous identity function, it's not useful enough to be given a seperate name.
  • 3
    @theabbie I was trying to be language agnostic. Whether you name it or implement it inline as an anonymous function is irrelevant, it's still the identity function. This is especially true because it's a pure function, so it has referential transparency. And "id" is a pretty standard name for it in the FP community and in programming language theory, so your claim of it not being useful enough to name is pretty shifty.
  • 0
    TFW when your CS teacher says word limit is 500
  • 1
    When you write a function f(x) that returns g such that g(h) = h(x)
    That will be funky code.
    The identity function is nothing special, it's mainly for indicating to a HOF that you do not intend to do anything.
  • 1
    Oh wait, was that a pun? Funcy? Ohhhhhh.
  • 2
    Thanks to the mention bug, I didn’t see this.

    Why not have the identity function be the default behavior @RememberMe?

    I’m probably too sick to think this through well enough.
Add Comment