Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active April 2, 2023 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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/8971ff54b9871776cf82f70ecba695a50fdb1dcf
// 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.1.1"
// ---------------------
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