• 0 Posts
  • 17 Comments
Joined 11 months ago
cake
Cake day: August 23rd, 2023

help-circle

  • We let almost all manufacturing jobs go overseas just to cut labor costs and now we’re suffering the consequences and our government completely incapable of doing what’s necessary to bring that manufacturing capability back to the US. At this point basic Keynesians economic policy is tantamount to heresy for anyone but the far left. Its like we’ve adopted the economic policies we forced on third world nations, and found ourselves with a third world economy.

    Being able to produce cheap drones as good as DJIs is far more important for national security than whatever espionage risk they pose. Cheap, easy to use, drones like the dji phantom are omnipresent in current wars. Banning them prevents us from learning via competition or basic reverse engineering.












  • Very standard use case for a fold or reduce function with an immutable Map as the accumulator

    val ints = List(1, 2, 2, 3, 3, 3)
    val sum = ints.foldLeft(0)(_ + _) // 14
    val counts = ints.foldLeft(Map.empty[Int, Int])((c, x) => {
      c.updated(x , c.getOrElse(x, 0) + 1)
    })
    

    foldLeft is a classic higher order function. Every functional programming language will have this plus multiple variants of it in their standard library. Newer non-functional programing languages will have it too. Writing implementations of foldLeft and foldRight is standard for learning recursive functions.

    The lambda is applied to the initial value (0 or Map.empty[Int, Int]) and the first item in the list. The return type of the lambda must be the same type as the initial value. It then repeats the processes on the second value in the list, but using the previous result, and so on until theres no more items.

    In the example above, c will change like you’d expect a mutable solution would but its a new Map each time. This might sound inefficient but its not really. Because each Map is immutable it can be optimized to share memory of the past Maps it was constructed from. Thats something you absolutely cannot do if your structures are mutable.