Skip to content

Instantly share code, notes, and snippets.

View malzzz's full-sized avatar

Mallory M. malzzz

  • Asymmetric Finance
  • /dev/null
View GitHub Profile

10 Scala One Liners to Impress Your Friends

Here are 10 one-liners which show the power of scala programming, impress your friends and woo women; ok, maybe not. However, these one liners are a good set of examples using functional programming and scala syntax you may not be familiar with. I feel there is no better way to learn than to see real examples.

Updated: June 17, 2011 - I'm amazed at the popularity of this post, glad everyone enjoyed it and to see it duplicated across so many languages. I've included some of the suggestions to shorten up some of my scala examples. Some I intentionally left longer as a way for explaining / understanding what the functions were doing, not necessarily to produce the shortest possible code; so I'll include both.

1. Multiple Each Item in a List by 2

The map function takes each element in the list and applies it to the corresponding function. In this example, we take each element and multiply it by 2. This will return a list of equivalent size, compare to o

Revisiting Tagless Final Interpreters

Tageless Final interpreters are an alternative to the traditional Algebraic Data Type (and generalized ADT) based implementation of the interpreter pattern. This document presents the Tageless Final approach with Scala, and shows how Dotty with it's recently added implicits functions makes the approach even more appealing. All examples are direct translations of their Haskell version presented in the Typed Tagless Final Interpreters: Lecture Notes (section 2).

The interpreter pattern has recently received a lot of attention in the Scala community. A lot of efforts have been invested in trying to address the biggest shortcomings of ADT/GADT based solutions: extensibility. One can first look at cats' Inject typeclass for an implementation of [Data Type à la Carte](http://www.cs.ru.nl/~W.Swierstra/Publications/DataTypesA

@beala
beala / fibsMemo.hs
Created May 8, 2016 23:25
Elegant memoization in Haskell with laziness.
import Control.Monad.State.Strict
import qualified Data.Map.Strict as Map
-- This is the standard fibonacci implementation with exponential complexity.
naiveFibs :: Int -> Integer
naiveFibs 0 = 0
naiveFibs 1 = 1
naiveFibs n = naiveFibs (n-2) + naiveFibs (n-1)
-- For reference, calculating the 35th fibonacci takes 9.49 sec.
@non
non / transducers2.scala
Created January 25, 2015 16:52
Basic encoding of Transducers with a little fanciness
import spire.algebra._
import spire.implicits._
object Transducer {
type RF[R, A] = (R, A) => R
def apply[A, B](f: A => B) =
new Transducer[B, A] {
def apply[R](rf: RF[R, B]): RF[R, A] = (r, a) => rf(r, f(a))
}
@judotens
judotens / bitcoinkey.sh
Last active December 2, 2023 07:03
Scrape BitCoin private keys from directory.io
#!/bin/bash
# scrape all leaked bitcoin private keys into a tab separated text
# <private key>\t<bitcoin_address>
#
# support autoresume. just add these line into your cron : * * * * bash bitcoinkey.sh
# results stored on keys.txt
if [ ! -f last.page ]; then prev=`echo 0`; else prev=`cat last.page`; fi;
if [ -z $1 ]; then akhir=`echo 10`; else akhir=`echo $1`; fi;
abis=$(($prev+$akhir))
@djspiewak
djspiewak / 0introduction.md
Last active November 28, 2023 15:03
Scala Collections Proposal

Collections Redesign Proposal

I'm going to start off by motivating what I'm doing here. And I want to be clear that I'm not "dissing" the existing collections implementation or anything as unproductively negative as that. It was a really good experiment, it was a huge step forward given what we knew back in 2.8, but now it's time to learn from that experiment and do better. This proposal uses what I believe are the lessons we can learn about what worked, what didn't work, and what is and isn't important about collections in Scala.

This is going to start out sounding really negative and pervasively dismissive, but bear with me! There's a point to all my ranting. I want to be really clear about my motivations for the proposal being the way that it is.

Problems

Generic Interfaces

@KodrAus
KodrAus / Profile Rust on Linux.md
Last active November 14, 2023 17:19
Profiling Rust Applications

Profiling performance

Using perf:

$ perf record -g binary
$ perf script | stackcollapse-perf.pl | rust-unmangle | flamegraph.pl > flame.svg

NOTE: See @GabrielMajeri's comments below about the -g option.

@RxDx
RxDx / MacOSLogoutHook.txt
Last active September 21, 2023 23:50
MacOS: Run script before shutdown
Create a file:
$ pico /Users/Shared/logoutHook.sh
File content:
#!/bin/bash
say 'Hasta la vista baby!'
Set exuction permission:
$ sudo chmod +x /Users/Shared/logoutHook.sh
@jkeefe
jkeefe / raspberry-pi-ble-sniffer.md
Last active September 16, 2023 14:33
Notes on using Adafruit's "Bluefruit" BLE sniffer with a Raspberry Pi

BLUETOOTH SNIFFER

Working to sniff Bluetooth Low Energy with the adafruit sniffer

For more information, see this blog post

Going the python route, as described here

before installing pySerial, did ...

@viktorklang
viktorklang / Future-retry.scala
Last active July 23, 2023 23:48
Asynchronous retry for Future in Scala
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import akka.pattern.after
import akka.actor.Scheduler
/**
* Given an operation that produces a T, returns a Future containing the result of T, unless an exception is thrown,
* in which case the operation will be retried after _delay_ time, if there are more possible retries, which is configured through
* the _retries_ parameter. If the operation does not succeed and there is no retries left, the resulting Future will contain the last failure.