GithubHelp home page GithubHelp logo

layers-kt's Introduction

Public Domain

Layers KT

build issues vulnerabilities license

An experiment in style and technique in Kotlin.

Layers is a list of maps that appears as a single map. It uses rules to provide a single value for each key, based on all values for that key in the list of maps. For example, if the key "BOB" has values 1, 2, and 3 in different layers, and the rule were "sum", then the value of "BOB" in the map would be 6. Changing the rule to "latest of" for "BOB" would result in the map having 3 for the value of that key.

(See Layers Java for an older approach in Java.)

Build and try

To build, use ./mvnw verify or ./batect build (for a CI-like experience).

There are no run-time dependencies.

Platform

This code relies on JDK 11 or newer.

Build

Use ./mvnw (Maven) or ./batect build (Batect) to build, run tests, and create a demo program. Use ./run.sh or ./batect run to run the demo.

Batect works "out of the box", however, an important optimization is to avoid redownloading plugins and dependencies from within a Docker container.

This shares Maven plugin and dependency downloads with the Docker container run by Batect.

API

General patterns

It is difficult to express in Kotlin the structure of functions without providing their names (think interfaces; meta-programming may be required), but for all functions which express map state (construction or mutation), functions come in three styles:

  • foo(vararg state: Pair<String, Entry<*>>): T
x = x.foo("a" to 3.toValue()) // Merge into existing mutable map
  • foo(state: EntryMap): T
x = x.foo(mapOf("a" to 3.toValue())) // Merge into existing mutable map
  • foo(state: MutableMap<String, Entry<*>>.() -> Unit): T
x = x.foo {
    this["a"] = 3.toValue() // Mutate existing mutable map
}

Example functions following these patterns include new and edit.

Actual declarations take advantage of these type aliases:

  • EntryMap = Map<String, Entry<*>>
  • EditMap = MutableMap<String, Entry<*>>
  • EditBlock = EditMap.() -> Unit

Layers

Layers is a subtype of Map .

Properties

  • layers — an immutable list of layers, ordered from most recent to oldest. The top-most in the list is the current layer
  • current — the current, editable layer. Make updates against the _ current_ layer

Layer

Layer is a subtype of Map .

Editable layer

EditableLayer is a subtype of Layer.

Methods

  • edit — edits with a block. All Map methods apply. A sample:
          editableLayer.edit {
              this["BOB"] = 3.toEntry()
          }
    

Standard rule: Constant

A layers rule which returns a constant value.

See ConstantRule .

Standard rule: Latest

A layers rule which returns the latest value (the value in the most recently layer to a Layers).

See LatestOfRule .

Standard rule: Sum

A layers rule which sums all values added to a Layers.

See SumOfRule .

TODO

  • Rationalize uses of Map as input vs MutableMap reused as a property or delegated argument
  • Incorporate the spike having generic key and value types

layers-kt's People

Contributors

binkley avatar boxleytw avatar

Watchers

 avatar  avatar  avatar

layers-kt's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.