Created
December 19, 2014 02:22
-
-
Save sumnulu/df8c6de9e07f70e68890 to your computer and use it in GitHub Desktop.
Scala 2
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
import util.Random._ | |
//Rastgele 1000 adet plaka data'nin türü List "benzeri" | |
val n = 1000 | |
val data = (1 to n) map (_ => nextInt(100) ) | |
// ilk bolum (ilk parantezi için) aynı sayıları guruplara boluyor ve | |
// Map[Int,List[Int]] türünde bir değer(value) oluşturuyor | |
// ikinci bölüm, groupBy'ın çıktısını Map[Int,Int] türüne çeviriyor (key'ler plaka, value'lar ise miktar) | |
// identity yerine { x => x} de yazabilirdik | |
val count = (data groupBy identity ) mapValues (_.size) | |
// String'in başında ki "s" ye dikkat edelim, String için'de $ karakteri ile değerlere erişebilmeyi sağlıyor | |
count foreach {case (plaka,count) => println(s"plaka = $plaka, plaka sayisi = $count, frekans = ${count.toDouble/n} ") } | |
/* çıktı... | |
plaka = 69, plaka sayisi = 10, frekans = 0.01 | |
plaka = 0, plaka sayisi = 8, frekans = 0.008 | |
plaka = 88, plaka sayisi = 14, frekans = 0.014 | |
plaka = 5, plaka sayisi = 9, frekans = 0.009 | |
plaka = 10, plaka sayisi = 13, frekans = 0.013 | |
plaka = 56, plaka sayisi = 10, frekans = 0.01 | |
plaka = 42, plaka sayisi = 10, frekans = 0.01 | |
.... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment