this may border on philosophical, thought right place ask.
suppose have function creates list of ids. these identifiers used internally application, acceptable use es2015 symbol()
here.
my problem that, technically, when ask symbol, i'd imagine js runtime creates unique identifier (random number? memory address? unsure) which, prevent collisions, require accessing global state. reason i'm unsure because of word, "technically". i'm not sure (again, philosophical standpoint) if ought enough break mathematical abstraction api presents.
tl;dr: here's example--
function sentineltosymbol(x) { if (x === -1) return symbol(); return x; }
is function pure?
not really, no, might not matter.
on surface, (foo) => symbol(foo)
appears pure. while runtime may operations side effects, never see them, if call symbol()
@ same time same parameters. however, calling symbol
same arguments never return same value, 1 of main criteria (#2, below).
from the mdn page:
note symbol("foo") not coerce string "foo" symbol. creates new symbol each time:
symbol("foo") === symbol("foo"); // false
looking solely @ side effects, (foo) => symbol(foo)
pure (above runtime).
however, pure function must meet more criteria. from wikipedia:
purely functional functions (or expressions) have no side effects (memory or i/o). means pure functions have several useful properties, many of can used optimize code:
- if result of pure expression not used, can removed without affecting other expressions.
- if pure function called arguments cause no side-effects, result constant respect argument list (sometimes called referential transparency), i.e. if pure function again called same arguments, same result returned (this can enable caching optimizations such memoization).
- if there no data dependency between 2 pure expressions, order can reversed, or can performed in parallel , cannot interfere 1 (in other terms, evaluation of pure expression thread-safe).
- if entire language not allow side-effects, evaluation strategy can used; gives compiler freedom reorder or combine evaluation of expressions in program (for example, using deforestation).
you argue preface list rules out everything in javascript, since operation result in memory being allocated, internal structures updated, etc. in strictest possible interpretation, js never pure. that's not interesting or useful, so...
this function meets criteria #1. disregarding result, (foo) => symbol(foo)
, (foo) => ()
identical outside observer.
criteria #2 gives more trouble. given bar = (foo) => symbol(foo)
, bar('xyz') !== bar('xyz')
, symbol
not meet requirement @ all. guaranteed unique instance every time call symbol
.
moving on, criteria #3 causes no problems. can call symbol
different threads without them conflicting (parallel) , doesn't matter order called in.
finally, criteria #4 more of note direct requirement, , met (the js runtimes shuffle around go).
therefore:
- strictly speaking, nothing in js can pure.
symbol()
not pure, example not either.- if care side effects rather memoization, example meet criteria.
Comments
Post a Comment