Scala

7 posts
Scala

ZIO Log Annotations Are Confusing

I was working on a logger that writes metrics every time a warning or an error is logged. I tried to provide additional metric tags depending on the log annotations. And it didn't work. I was adding an annotation and I could see its value somewhere deep in the stack trace but the annotations value was empty.

Turned out that ZIO has two log annotation mechanisms that work in parallel.

Scala

4 Ways to Fail at Measuring Durations

This post is a confession about multiple overlapping mistakes we made when trying to measure API request times. These errors went unnoticed for months because wrong statistics still look realistic. Hopefully, this post will help you avoid such fails.

Algorithms

Solution for Project Euler Problem #700: Eulercoin

Problem: Imagine a sequence 1504170715041707*n mod 4503599627370517 where n is an integer increasing from 1. Find the subsequence of this sequence where every next element is smaller than the previous one.

All the solutions I've read about include brute force calculations. I found a better one, and I can't stop myself from posting it.

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.