Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active May 25, 2024 10:20
Show Gist options
  • Save dacr/95cee277afd0fdd874dbe6c829830a05 to your computer and use it in GitHub Desktop.
Save dacr/95cee277afd0fdd874dbe6c829830a05 to your computer and use it in GitHub Desktop.
the identity function / published by https://github.com/dacr/code-examples-manager #c7cc3f50-abbe-40e8-a6dc-0182fef9d7fe/35bb672a5f5f554557df7593a1637b5e9b9728f0
// summary : the identity function
// keywords : scala, language-feature, fold, @testable
// publish : gist
// authors : David Crosson
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// id : c7cc3f50-abbe-40e8-a6dc-0182fef9d7fe
// created-on : 2021-04-06T07:46:48Z
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : scala-cli $file
// ---------------------
//> using scala "3.4.2"
// ---------------------
val in1 = Option("B")
val out1 = in1.fold("A")(identity)
assert(out1 == "B")
val in2 = List(1,2,3)
val out2 = in2.map(identity)
assert(out2 == in2)
val in3 = List(1,2,2,3,4,4,4,4)
val out3 = in3.groupBy(identity).view.mapValues(_.size).toMap
assert(out3 == Map(1 -> 1, 2 -> 2, 3 -> 1, 4 -> 4))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment