Into

sequence~ Into

Source:

Into can be used to turn se­quences back into other types.

into is the in­verse of iter(), mean­ing that tak­ing the res­ult of iter() and call­ing into(), yields the ori­ginal value.

So in a purely func­tional lan­guage, into(iter(v)) would be a no-op; since we are in javas­cript, this es­sen­tially im­ple­ments a poor mans shal­low copy for some types

const shal­low­copy = (v) => into(v, v.con­structor);

In­ter­face

`(T: Type/​Func­tion, v: Se­quence) => r: T

Laws

  • into(v, type(v)) <=> shal­low­clone(v)

Spe­cial­iz­a­tion notes

String: Uses to­String() on each value from the se­quence and con­cat­en­ates them into one string... Ob­ject: Ex­pects key/​value pairs; the keys must be strings; se­quences con­tain­ing the same key mul­tiple times and se­quences with bad key/​value pairs are con­sidered to be un­defined be­ha­viour. The key/​value pairs may be se­quences them­selves. Map: Same rules as for ob­ject. Set: Refer to ht­tps://​de­veloper.moz­illa.org/​en-US/​docs/​Web/​JavaS­cript/​Ref­er­ence/​Glob­al_Ob­jects/​Set

Ex­amples

Prac­tical uses of into in­clude con­vert­ing between types; e.g:

into({foo:  42, bar: 23}, Map) # Map { 'foo' => 42, 'bar' }
into(["foo", " bar"], String) # "foo bar"
into([1,1,2,3,4,2], Set) # Set(1,2,3,4)

Into is also use­ful to trans­form val­ues us­ing the func­tions in this class:

# Re­move odd num­bers from a set
const st = new Set([1,1,2,2,3,4,5]);
into(fil­ter(st, n => n % 2 == 0), Set) # Set(2,4)

# Re­move a key/​value pair from an ob­ject
const obj = {foo: 42, bar: 5};
into(fil­ter(obj, ([k, v]) => k !== 'foo'), Obj)
# yields {bar: 5}

It can be even used for more com­plex use cases:

# Merge mul­tiple key/​value con­tain­ers into one se­quence:
const seq = con­cat([[99, 42]], new Map(true, 23), {bar: 13});
into(seq, Map) # Map( 99 => 42, true => 23, bar => 13 )