Skip to content

Instantly share code, notes, and snippets.

View Cotel's full-sized avatar
💪
Working hard

Miguel Coleto Cotel

💪
Working hard
View GitHub Profile
@Cotel
Cotel / ListAdapterBuilder.kt
Created September 26, 2020 17:29
A DSL for creating `ListAdapter`s
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
fun <T, VH : ListAdapterBuilder.ViewHolder<T>> createListAdapter(
init: ListAdapterBuilder<T, VH>.() -> Unit
import arrow.core.Either
import arrow.core.left
import arrow.core.right
class JsonUser
class User
fun JsonUser.toUser(): User = throw RuntimeException()
sealed class UserQueryErrors {
@Cotel
Cotel / RefinedTypes.kt
Last active November 25, 2018 10:35
How to make refined types in Kotlin with Arrow (https://arrow-kt.io/)
import arrow.core.Option
class NonZeroInt private constructor(val value: Int) {
companion object {
operator fun invoke(x: Int): Option<NonZeroInt> =
if (x == 0) Option.empty() else Option.just(NonZeroInt(x))
fun get(x: NonZeroInt): Int = x.value
}
}
import {combineReducers} from 'redux'
const todos = (state = [], {type, payload}) => {
switch(type) {
case LIST_TODOS:
return payload
case ADD_TODO:
return {...state, payload}
case TOGGLE_TODO:
return state.map(todo => todo.id === payload ? {...todo, completed: !todo.completed} : todo)
const initialState = {
todos: [],
filter: 'all',
isLoading: false
}
export default function(state = initialState, action) {
switch (action.type) {
case FETCH_TODOS:
return {...state, isLoading: true}
@Cotel
Cotel / DeepTransformation.js
Created January 30, 2018 17:03
An example of an immutable transformation of a deep object.
const restaurant = {
clientRequests: {
"1": {
"drinks": [
{id: 3, name: "Beer", quantity: 4}
]
},
"2": {
"appetizers": [
{id: 1, name: "Onion rings", quantity: 2},
@Cotel
Cotel / Redux example.js
Created January 26, 2018 17:59
A Redux example
const store = {
partyMembers: []
}
const addPersonToParty = (person) => ({type: "ADD_PERSON_TO_PARTY", payload: person})
const removePersonFromParty = (person) => ({type: "REMOVE_PERSON_FROM_PARTY", payload: person})
const reducer = (state = store, action) => {
switch (action.type) {
case "ADD_PERSON_TO_PARTY":