Functional Programming

7 posts
TypeScript

A Good Way How to Validate Types in TypeScript

TL;DR: I've created a typescript library fp-ts-type-check. You can use it to validate the structure of data you get from outside of your application.

Here's the situation: your typescript application needs to retrieve some object via some API. You make an API request and you get a successful response with some json. Probably it's the data you expect but you can't say for sure. Your actions?

A Story About Rubber Ducks and Functional Programming

Once upon a time, I wanted to get a rubber duck. One rubber duck. I spent a lot of time looking for a perfect yellow rubber duck not representing any character and finally, I have found and bought it.

Later I accidentally found a much better rubber duck. I bought it also. That's one more rubber duck than I but I wasn't cruel enough to throw first one away. A big mistake.

Scala

Functional FizzBuzz in Scala With Streams and Higher Order Functions

I'm currently learning functional programming with scala and as a practice I've implemented FizzBuzz. To be honest, I made three different implementations of FizzBuzz but only the third one is good.

Stream.from(1) # Create infinite lazy stream from 1
  .map { (_, "") } # convert it to (number, word) tuple
  .map { x => if (x._1 % 3 == 0) (x._1, x._2 + "Fizz") else x } # Add "Fizz" to each 3rd word
  .map { x => if (x._1 % 5 == 0) (x._1, x._2 + "Buzz") else x } # Add "Buzz" to each 5th word
  .map { x => if (x._2 != "") x._2 else x._1.toString } # Take word or number
  .take(30) # Limit stream length
  .foreach(println) # Run everything and print results

It's even much better than usual imperative implementation and here's why.