Mutator for Immutable Object

Last update: Jun 12, 2022

Mutator

Why Mutator?

As the immutable object becomes the default pattern of functional programming language, such as Value Object in Java, and Data Class in Kotlin, even the immutable pattern has a lot of advantages, modifying properties of deeply nested immutable objects is still not very convenient, for example:

data class Person(
    val name: String,
    val address: Address
)

data class Address(
    val nation: Nation,
    val state: String,
    val street: String
)

data class Nation(
    val name: String,
    val code: String
)

If we want to modify the Person.address.nation.code, we have to write code like this:

val person = Person(...)
// ...
val clone = person.copy(
    address = person.address.copy(
        nation = person.address.nation.copy(
            code = "New Code"
        )       
    )
)

Getting Started

  1. Add dependency into build.gradle or build.gradle.kts

    dependencies {
        implementation("io.johnsonlee:mutator:1.2.0")
    }
  2. Call the extension function mutate(...)

    val clone = person.mutate(Person::address, Address::nation, Nation::code, "New Code")

    The code above is equivalent to the following

    val clone = person.copy(
       address = person.address.copy(
           nation = person.address.nation.copy(
               code = "New Code"
           )       
       )
    )

GitHub

https://github.com/johnsonlee/mutator
You might also like...

Its a small example with Custom Employee object is sent from one Fragment to another.

Its a small example with Custom Employee object is sent from one Fragment to another.

Kotlin-SafeArgs-Custom-Object Its a small example with Custom Employee object is sent from one Fragment to another. Dependencies used // Navigation

Oct 30, 2021

Kotlin Android object for global applicationContext

ContextProvider Kotlin Android object for global applicationContext Usage In your Aplication class class YourApp : Application() { override fun o

Nov 4, 2021

Object declarations Board game basics Game

Object-declarations-Board-game-basics-Game You want to create a game and you decided to store the playing field in a PlayingField singleton. Inside th

Nov 7, 2021

EVMapper: Simple object mapper for Kotlin

EVMapper - Simple object mapper for Kotlin Simple mapping data class AData(val p1: Int, val p2: Int) data class BData(val p1: Int, val p2: Int) val a

Jan 24, 2022

Object-streams - An experimental library for dealing with legacy code

Object Streams Super experimental! no guarantees to the public API surface. At t

Jan 18, 2022

Based on Android + NodeMCU platform to achieve object detection and tracking, AI capabilities come from Google MLKit.

Based on Android + NodeMCU platform to achieve object detection and tracking, AI capabilities come from Google MLKit.

Based on Android + NodeMCU platform to achieve object detection and tracking, AI capabilities come from Google MLKit.

Apr 4, 2022

Demonstration of Object Pool Design Pattern using Kotlin language and Coroutine

Demonstration of Object Pool Design Pattern using Kotlin language and Coroutine

Object Pool Design Pattern with Kotlin Demonstration of Thread Safe Object Pool Design Pattern using Kotlin language and Coroutine. Abstract The objec

Apr 12, 2022

Pdf Extractor - Covert array list of object to pdf table

Pdf Extractor - Covert array list of object to pdf table

Pdf Extractor - Covert array list of object to pdf table

Apr 3, 2022

📭 Extension to Ktor’s routing system to add object oriented routing and much more. 💜

📭 Ktor Routing Extensions Extension to Ktor’s routing system to add object-oriented routing and much more. 💜 Why? This extension library was created

May 8, 2022

ShapeShift️ - A Kotlin library for intelligent object mapping and conversion between objects

ShapeShift️ - A Kotlin library for intelligent object mapping and conversion between objects

ShapeShift️ A Kotlin library for intelligent object mapping and conversion between objects. Documentation Installation Maven Gradle Groovy DSL Kotlin

Jun 3, 2022
Demonstration of Object Pool Design Pattern using Kotlin language and Coroutine
Demonstration of Object Pool Design Pattern using Kotlin language and Coroutine

Object Pool Design Pattern with Kotlin Demonstration of Thread Safe Object Pool Design Pattern using Kotlin language and Coroutine. Abstract The objec

Apr 12, 2022
Kotlin Symbol Processor library to create Mutable and Immutable variants of objects.

implier Kotlin Symbol Processor plugin to create Mutable and Immutable variants of objects. Examples @ImmutableImpl @MutableImpl public interface Samp

Mar 17, 2022
Object-relational mapping for Android

RushOrm Object-relational mapping for Android RushOrm replaces the need for SQL by mapping java classes to SQL tables. What is the aim? The aim is to

Jan 7, 2022
Helper object for injecting typeface into various text views of android.

TypefaceHelper Helper object for injecting typeface into various text views of android. Overview We can use various custom typefaces asset for any tex

May 22, 2022
An Android project containing image recognition and object detection models.
An Android project containing image recognition and object detection models.

An Android project containing image recognition and object detection models. Users can input images into the deep learning model by taking photos, opening photo albums, and real-time previews on the Android side. After the calculation on the Android side is completed, the model will output the prediction result and show it to the user.

Feb 26, 2022
This app using Mlkit along with the TensorFlow Lite model for object detection,

I built this app using Mlkit along with the TensorFlow Lite model for object detection, Arcore is used to place anchors to the detected objects. It's a good blend of Machine learning and Augmented reality to visualise ML information in a much better way than regular bounding boxes

Apr 30, 2022
This repo contains examples of all the functionalities of sceneform, Face filter , Object Placement etc

ARCore-Projects This repo uses sceneform 1.17.1 to demonstrate the usage of different features of arcore such as, Augmented Faces Augmented images AR

Jun 9, 2022
TensorFlow Lite Object Detection Android Demo
TensorFlow Lite Object Detection Android Demo

GSoC Project 2021 - TensorFlow Description This repository contains the project where I contributed to the TensorFlow Team during GSoC in the year 202

Jan 17, 2022
Animation samples with motion layout and object animator 🦹🏻‍♀️
Animation samples with motion layout and object animator 🦹🏻‍♀️

?? Animations ?? Animation samples with motion layout and object animator ????‍♀️ Car Animation with Object Animator ?? ?? I've created this project b

Jun 15, 2022
Kotlin Object Notation - Lightweight DSL to build fluid JSON trees

Kotlin Object Notation Lightweight kotlin MPP DSL for building JSON trees Setup Just drop the dependency in your commonMain sourceSet kotlin { sourc

Mar 28, 2022