Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
@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. -
killames5733yDoes it return something else with the input or does it modify data and then return so something knows what item it was acting on ?
-
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. -
CptFox16193yEver 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 -
@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.
-
@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.
-
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. -
Root825573yThanks 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.
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.
rant