Last active
February 3, 2026 20:24
-
-
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/ade93ac49de56b5bf2ce19af0a61e1d36954a75e
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // summary : the identity function | |
| // keywords : scala, language-feature, fold, @testable | |
| // publish : gist | |
| // authors : David Crosson | |
| // license : Apache License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt) | |
| // 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