Skip to content

Instantly share code, notes, and snippets.

View ch8n's full-sized avatar
💻
Always on to writing my next article.

Chetan Gupta ch8n

💻
Always on to writing my next article.
View GitHub Profile
@ch8n
ch8n / pair_to_map.kt
Created October 12, 2020 12:31
pair to map
// Pair Iterable has a extension which convert
// List<Pair<String,Int>> to Map
fun <K, V> Iterable<Pair<K, V>>.toMap(): Map<K, V>
// other implementation with d
@ch8n
ch8n / pair_signature.kt
Created October 12, 2020 12:31
Pair Signature
data class Pair<out A, out B> : Serializable
@ch8n
ch8n / entries_to_pair.kt
Created October 12, 2020 12:28
entries to Pair
// enteries to map example
val entries:Set<Entry<String, Int>> = setOf(entry)
val map = entries.map { entry -> entry.toPair() }.toMap()
// here, entries.map { entry -> entry.toPair() }
// is List<Pair<String,Int>> i.e Iterable<Pair<K, V>>.toMap()
@ch8n
ch8n / iterable_pair_to_map.kt
Created October 12, 2020 12:27
Iterable pair to map
fun <K, V> Iterable<Entry<K, V>>.toMap(): Map<K, V> Or any other isn't supported
// but instead there is
fun <K, V> Iterable<Pair<K, V>>.toMap(): Map<K, V>
@ch8n
ch8n / entry_to_pair.kt
Created October 12, 2020 12:26
entry to pair
fun <K, V> Entry<K, V>.toPair(): Pair<K, V>
@ch8n
ch8n / map_creation.kt
Created October 12, 2020 12:24
map creation functions
fun <K, V> mapOf(): Map<K, V>
fun <K, V> mapOf(pair: Pair<K, V>): Map<K, V>
fun <K, V> mapOf(vararg pairs: Pair<K, V>): Map<K, V>
@ch8n
ch8n / entries_body.kt
Created October 12, 2020 12:23
entries body maps in kotlin
val map = object : Map<String, Int>{
override val entries: Set<Entry<String, Int>>
get() = //stuff...
}
@ch8n
ch8n / entry_signature.kt
Created October 12, 2020 12:22
entry_signature maps in kotlin
// signature
interface Entry<out K, out V>
// Parameters
// K - the type of key
// V - the type of map values
@ch8n
ch8n / map_signature.kt
Created October 12, 2020 12:20
map signature kotlin maps
// signature
interface Map<K, out V>
// Parameters
// K - the type of key
// V - the type of map values

FWIW: I'm not the author of the content presented here (which is an outline from Edmond Lau's book). I've just copy-pasted it from somewhere over the Internet, but I cannot remember what exactly the original source is. I was also not able to find the author's name, so I cannot give him/her the proper credits.


Effective Engineer - Notes

What's an Effective Engineer?