27
loading...
This website collects cookies to deliver better user experience
Boxing is more like wrapping some value of type of x
in a container that is type of y
which has more functionalities that x
can use - Some panda
x
, for simplicity, let's say that x
is the well known type string
, so our value may be name = 'ahmed osama'
'ahmed osama'
of type string
in some other container of type y
to have more functionality we may need in our string type.array
object to wrap our object within?let name = 'Ahmed osama'
console.log(name.length) // 11
console.log(name.includes('e')) // true
Everything in JavaScript is an object
let myTrue = new Boolean(true)
let name = new String('ahmed osama')
console.log(typeof myTrue) // object
console.log(typeof name) // object
a
itself is an object of type String
or the identifier name
itself is the object and it contains within it some primitives?Primitive data types in JavaScript are not objects, but can be treated as if they were ones due to boxing, which gives a feeling that everything in javascript is object-like.
name.includes('e')
, the string ahmed osama
was wrapped in another type that has this method includes
.const someString = new String('abc')
const someArray = new Array(15)
console.log(someString.__proto__)
console.log(someArray.__proto__)
String.prototype
, so your primitive string is some how turned into it's object form so you can acess these methods.// First lets define the acceptable types
type Primitive =
| string
| number
| boolean
| undefined
| null
type JsonArray = Json[]
type JsonObject = { [k: string]: Json }
type Json = JsonObject | JsonArray | Primitive
// Next, let's define our Box
const Box = <T extends Json>(x: T) => ({
map: <U extends Json>(f: (v: T) => U) => Box(f(x)),
filter: (f: (v: T) => boolean) =>
f(x) ? Box(f(x)) : Box(undefined),
fold: (f: (v: T) => T) => f(x),
})
JSON
type is anyway, and returning an object that contains multiple functions/operations that operate against the given value.map
and filter
operations within the box, surely you can provide any desired operation.fold
operation is just a function to extract out the value after manipulation outside of the box.let myName = Box('ahmed osama')
.map((v) => v.toUpperCase())
.map((v) => v.split(''))
.map((v) => v.sort())
.map((v) => v.join(' '))
.map((v) => v.trim())
.fold((x) => x)
Well first, we're creating a box around the value 'ahmed osama'
so we can have more functionality.
Then we're mapping the string
to the uppercase form of it, note that this map
function we have is a little bit different the regular map function that's provided within the language, as it doesn't wrap the output in an array, it returns it with the same type provided.
Afterwards, we're once again mapping the returned caplitalized string to split it into its characters
We're sorting the array of characters so we have my name in sorted chars.
We're joining it back to a string separated by spaces
Them we trim the output as we have trailing spaces
Finally we fold the ouput a.k.a extracting it out of the box by applying the identity function
console.log(myName) // A A A D E H M M O S
string
in an object container that has mutliple functions that can operate against this string
type to ahcieve something.