Categories
discuss

How do you sort an array of words with Ramda?

Sorting numbers is easy with Ramda.

const sizes = ["18", "20", "16", "14"]
console.log("Sorted sizes", R.sort((a, b) => a - b, sizes))
//=> [ '14', '16', '18', '20' ]

Sorting an array of words with vanilla javascript is too.

const trees = ["cedar", "elm", "willow", "beech"]
console.log("Sorted trees", trees.sort())

How would you sort an array of words with Ramda.
If you had to.

const trees = ["cedar", "elm", "willow", "beech"]
console.log("Sorted trees", R.sort((a, b) => a - b, trees))
//=> ["cedar", "elm", "willow", "beech"]

Answer

import R from 'ramda'

const names = ['Khan', 'Thanos', 'Hulk']

const sortNamesAsc = R.sortBy(R.identity) // alphabetically
const sortNamesDesc = R.pipe(sortNamesAsc, R.reverse)

sortNamesAsc(names) // ['Hulk', 'Khan', 'Thanos']
sortNamesDesc(names) // ['Thanos', 'Khan', 'Hulk']

Ramda Repl example

Categories
discuss

listOf() returns MutableList

In Kotlin, a list created using the listOf() function (which should be immutable) passes a type check against MutableList using the is operator.

Example:

fun main(args: Array<String>) {
    val list = listOf("I'm immutable")

    println(list is MutableList)
}

will print

true

Interestingly, an empty list created using listOf<String>() will fail the check and print false as it returns the singleton object EmptyList.

After some digging, it turns out that mutableListOf() creates a java.util.ArrayList whilst listOf() ends up creating a java.util.Arrays$ArrayList, but none of the classes involved implement MutableList, so why does a non-empty list still pass the type check against it? Hence, is there an alternative way of reliably checking if a list is mutable without having to check against it’s actual implementation (is ArrayList etc.)?

Answer

The separation between List and MutableList is an illusion created by the Kotlin compiler. At runtime, Kotlin uses Java collection classes, which only have a single List interface containing both read and mutation methods. Compile-time references to List and MutableList are both compiled to java.util.List references. Therefore, it’s not possible to detect whether a list is a MutableList at runtime.

Categories
discuss

Preference library version 28 creates huge padding on the left side of each preference

I updated the com.android.support:preference library from version 27 to 28 and ever since I did that, there is a huge amount of padding to the left of each preference. It’s almost like it’s assuming that there will be icons there, but I have no icons specified. See screenshot below. Is there a way to get rid of this padding?

PreferenceScreen layout

Answer

Try set:

app:iconSpaceReserved="false"
Categories
discuss

Android studio gradle build failed with update

I recently updated android studio to version 3.2. When I tried running my old project, I got the following message for my old project while running grade build:

"The Android Gradle plugin supports only Kotlin Gradle plugin version 1.2.51 and higher.

Project ‘4thtry’ is using version 1.2.50.” How to rectify it?

Answer

Go to your root Build.gradle and update the Kotlin.

(Better idea): Add the latest version: ext.kotlin_version = '1.2.71' (or above of 1.2.50) instead of 1.2.50 then rebuild the project.

So we’ll have;

buildscript {
    ext.kotlin_version = '1.2.71'
    repositories {
..
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
...
}
Categories
discuss

How to deserialize generic class members with moshi?

I am fetching a JSON object which contains a generic member (data can be of a few different types). The class currently looks like this:

@Parcelize
data class Children<T: Parcelable>(
        @Json(name = "type") val type: String,
        @Json(name = "data") val data: T
): Parcelable

How do I go about being able to deserialize/map the correct object type with moshi?

@Parcelize
data class Comment<T : Parcelable>(
    @Json(name = "replies") val replies: Children<T>,
    @Json(name = "count") val count: Int,
    @Json(name = "children") val childs: List<String>

) : Parcelable

Or how about instances such as this? I should note Comment can take a generic param of Comment thus resulting in a loop.

Answer

Add below inlines in an MoshiExtensions and try to use them accordingly.

inline fun <reified E> Moshi.listAdapter(elementType: Type = E::class.java): JsonAdapter<List<E>> {
    return adapter(listType<E>(elementType))
}

inline fun <reified K, reified V> Moshi.mapAdapter(
        keyType: Type = K::class.java,
        valueType: Type = V::class.java): JsonAdapter<Map<K, V>> {
    return adapter(mapType<K, V>(keyType, valueType))
}

inline fun <reified E> listType(elementType: Type = E::class.java): Type {
    return Types.newParameterizedType(List::class.java, elementType)
}

inline fun <reified K, reified V> mapType(
        keyType: Type = K::class.java,
        valueType: Type = V::class.java): Type {
    return Types.newParameterizedType(Map::class.java, keyType, valueType)
}
Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..