5

Playing with functional style in Javascript:

Comments
  • 1
    @SortOfTested Can it be simpler?
  • 3
    You could filter for window.visible === true and just map the window.raise().

    Please add braces to one lineblock if's. It saves lives.
  • 0
    @IntrusionCM

    theDead.filter(window => window.visible === true).map(function(window){window.raise()})
  • 0
    @IntrusionCM Maybe this is better:

    theDead.filter(window => window.visible === true).map(window => window.raise())
  • 0
    @Demolishun Yip.

    Not sure what the first undefined check does, JS was long time ago...

    But you don't even need the variable assignment.

    theDead
    .filter(window => window.raised !== undefined && window.visible === true)
    .map(window => {window.raise();})

    In ES6 / arrow
  • 0
    @IntrusionCM I have objects that are referenced the get deleted. I could clean those up explicitly elsewhere, but for fun I was clearing them out this way. I had expected their reference to be removed from the list, but that is not the case. This is not in a browser I am not sure if this would be garbage collected if it was in a browser.
  • 4
    Simpler, probably. From a functional perspective though, I think the more important things to tweak in this are:

    1. Make the functions pure

    https://medium.com/javascript-scene...

    2. Consider using point free style to make the calls more composable

    https://medium.com/dailyjs/...

    3. Replace the side effects with function calls
    will need to do some examples for this one, it's a bit of a major tweak

    4. Embrace immutability, will naturally lead to the implementation of the previous suggestions
    https://medium.com/cloudverse/...

    Overall, javascript is kind of flakey when it comes to FP. It always turns down to pretending really hard. Purescript is better, Ramda and Rx provide the plumbing you would usually have to write yourself.

    Personally, I'd start with OCaml or F#. They're less messy in that regard.
  • 5
    @Demolishun
    This is how I would write it. It's definitely not shorter, but it's typographically correct and a continuous stream with no side effects or mutable fields. Added a bunch of comments to clarify what is going on:
    https://stackblitz.com/edit/...
  • 1
    @SortOfTested damnit with your new background colour and me using pixelated avatars it takes me a while to differentiate you from @Voxera. Did a few double takes already. Especially since both of you write good stuff.
  • 0
    @RememberMe thank you :)
Add Comment