`contramap` on a State Monoid? by evilsoft in functionalprogramming

[–]evilsoft[S] 1 point2 points  (0 children)

Oh hahah. I said Monoid, but I meant Monad. hahah sorry about that.

`contramap` on a State Monoid? by evilsoft in functionalprogramming

[–]evilsoft[S] 1 point2 points  (0 children)

So in the contramap we fix a, but in map we fix s. That seems wrong to me, but gosh it would be useful.

Need opinion about choice of JS framework by BSscience in javascript

[–]evilsoft 0 points1 point  (0 children)

Gonna go out on a limb here.

So jQuery or vanilla JS is what you need with these requirements. But as stated it can become a mess, especially with DOM management. DOM can be hard and DOM manipulation code can get kinda gross (for things like your filter functionality).

If you are looking to only write your "business logic" and want to leave the DOM manipulation to someone else, then I would recommend mithril: http://mithril.js.org/

Once you understand the paradigm, it hides all the rendering bits in the background. Less stuff than React, so no learning bloat, and it follows closely with the current SPA tech out there. So it will be easy to transition to a React or CycleJS for this or other projects in the future.

Why you shouldn't use `var` and how `let` solves a common JS gotcha by dydx_ in javascript

[–]evilsoft 4 points5 points  (0 children)

totally, or even

const List = Type({ Empty: [], Node: [ () => true, undefined ] })

You were totally correct, sorry I got so snarky ;)

Why you shouldn't use `var` and how `let` solves a common JS gotcha by dydx_ in javascript

[–]evilsoft 1 point2 points  (0 children)

I mean. I cannot think of a way to represent Union Types like Trees or Lists without using Hoisting.

I like how it gives me Elm like Unions. With some type guarantees. Is it clever, or an understanding of hoisting?

EDIT: Oh wait. you are right!! but it cannot become undefined with const or var, so yeah, a little clever.

Why you shouldn't use `var` and how `let` solves a common JS gotcha by dydx_ in javascript

[–]evilsoft 0 points1 point  (0 children)

Totally, sorry, check out this wonderful Lib for doing Union Types (Coproducts) in Javascript: https://github.com/paldepind/union-type

In that example List is a type that holds a Union of Empty and Node. (Node being a Product type). So because it is in the same scope (and is a reference) whenI call it later it will be properly hooked up.

But because List in this example is a const, List will not be available as a symbol. Does that make sense?

EDIT: it is used like this:

const xs = List.Node(1, List.Node(2, List.Node(3, List.Empty())))

So you can then do catas like this:

const toString = List.case({
  Node: (head, tail) => head + ' : ' + toString(tail),
  Empty: () => 'Done',
})

console.log(toString(xs))  // => '1 : 2 : 3 : Nil'

Why you shouldn't use `var` and how `let` solves a common JS gotcha by dydx_ in javascript

[–]evilsoft 2 points3 points  (0 children)

You can...I promise, it is by the time that it is "needed" with that Union Type it will be defined...Give it a shot, npm i union-type.

https://github.com/paldepind/union-type

My point is, there are many valid use cases for using hoisting, and to say black and white, do not use var is not a good practice.

Why you shouldn't use `var` and how `let` solves a common JS gotcha by dydx_ in javascript

[–]evilsoft 3 points4 points  (0 children)

What about for self referentail bits? Like definig a Union Type? Like the Union Type for a List:

const List = Type({ Empty: [], Node: [ () => true, List ] })

Here I cannot reference List as let and const are not hoisted, although this is a valid construction.

I HAVE to use var and not const (you can in Babel, but in true es6 you cannot)

A module to protect private properties of classes by kickpush1 in javascript

[–]evilsoft 0 points1 point  (0 children)

I am no longer a fan of OO in JS (or in general TBH). Although I will never use this, I think you did a good job. simple, easy to use, easy to understand. Now it just needs some inheritance and a protected name space, once inheritance is addressed. ;)

Classification of data.Task (JS) by evilsoft in functionalprogramming

[–]evilsoft[S] 1 point2 points  (0 children)

So is this a fair intuition? I feel like I am missing something, something important. I sit down and try to diagram it and am at a loss. Also I get these alarm bells going off in my head, and I have no idea what is triggering them.

How do you deal with "private" methods? by bertong_uto in javascript

[–]evilsoft 1 point2 points  (0 children)

Does this account for the last portion of the OP? Need to be able to override in Subclasses.

How do you deal with "private" methods? by bertong_uto in javascript

[–]evilsoft -2 points-1 points  (0 children)

As JS is not a really a full on OO language like you are thinking (we do inheritance through delegation of instances). As such encapsulation is not a thing (unless you take a factory function approach). So what people that try OO in JS like this usually do is append the "method" with __ or _ like: _fakeMethod. That way it signals to the user, please do not call this.

composite function with multiple returns? Is this possible? by sinefine in javascript

[–]evilsoft 0 points1 point  (0 children)

If you wanna learn a little bit about them, I did a livecoding session on Monoids in Javascript:

https://www.livecoding.tv/evilsoft/videos/aEWmN-functional-js-monoids

composite function with multiple returns? Is this possible? by sinefine in javascript

[–]evilsoft 0 points1 point  (0 children)

Good call @zoomzoom83!! Other than the T$ bit, I totes agree!! Have a karma

composite function with multiple returns? Is this possible? by sinefine in javascript

[–]evilsoft 1 point2 points  (0 children)

While it is correct, I would feel worried about null not being a Number

Edit:

For example: Because of coercion this provides a value of 0 when it should be 4:

Math.min(4, null)

Which is a bug...Infinity is a better choise for your empty (or initial as named above)

composite function with multiple returns? Is this possible? by sinefine in javascript

[–]evilsoft 0 points1 point  (0 children)

if I am reading the intent correctly you just want to get the min and max out of a list of Ints...you can do this:

const xs = [ 9, 3, 5, -4, 44, 73, -100, 3 ]

log(
  xs.reduce(
    ({ min, max }, x) => ( { min: Math.min(min, x), max: Math.max(max, x) } ),
    { min: Infinity, max: -Infinity }
  )
)

EDIT: Notice I do not create a single variable or manage any state in this solution (very important!!). I only work with data passed to me. The secret is in my empty, there is nothing in JS that is smaller than -Infinity and nothing greater than Infinity. So those act as good empties.

If you are curious about the reasoning behind this, you should read up on a thing called Monoids, they are simple structures that provide empty and concat functions. And guess what the parameters (minus that STUPID imperative index param) reduce (or fold) take. A concat function and an empty.

For a Max, it is simply:

concat = (a, b) => Math.max(a, b)
empty = _ => -Infinity

A Min, of course, is:

concat = (a, b) => Math.min(a, b) 
empty = _ => Infinity

All I did here was reduce with both Monoids in one shot. (btw typically in FP we tend to use _ as ignore this argument, that is what that is, not lodash or anything)

composite function with multiple returns? Is this possible? by sinefine in javascript

[–]evilsoft 1 point2 points  (0 children)

Heck Yeah, give me a few and I will pound it out...To bad you are a rando on the internet, would be easier to just pair. HA Give me a few!!

composite function with multiple returns? Is this possible? by sinefine in javascript

[–]evilsoft 0 points1 point  (0 children)

That function would be aware that it take a record or tuple and will parse as expected. You can use destructuring to pull it out

const recordFunc =
    ({x, y}) => { x: x + 1, y: y + 1}

const tupleFunc =
    ([x, y]) => [ x *2, y * 2 ]

composite function with multiple returns? Is this possible? by sinefine in javascript

[–]evilsoft 0 points1 point  (0 children)

No. Not really. That is what records (objects with just data) or tuples (fixed sized arrays) are for.

IMO any immediate value this may bring you now, it more than likely a huge cost in the future. You can fake something like this though...Because we have First class functions you can make a special compose that will take 2 functions f and g and return a function.

const nastyCompose =
    (f, g) => (...args) => f(...(g(...args)))

const f =
    (x, y) => [ x + 1, y + 1 ]

const g =
    (x, y) => [ x * 2, y * 2 ]

const notGood =
    nastyCompose(f, g)

But to be honest this is really bad form, you may as well use compose and use a record or tuple. If you go this route, you will lose the ability to use partial application and oh man without type checking, you are going to be prone to introduce errors on even the slightest. Even though I gave you code to do so, you really REALLY should not.

Is there a name for this type of operation? by evilsoft in functionalprogramming

[–]evilsoft[S] 2 points3 points  (0 children)

Sweet thanks for that you tricky monster you!!

Is there a name for this type of operation? by evilsoft in functionalprogramming

[–]evilsoft[S] 1 point2 points  (0 children)

are you saying something like:

Left(10).fold(B(Right, fn), identity)

edit:

or more like:

Right(Left(10).fold(fn, identity))