Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

You can come up w/ an algebra for a specific problem domain by thinking about the data models and repurposing some simple abstract algebra.

E.g.:

    new(state, &args) => mutation
    apply(state, mutation) => state'
Applied to finance:

    newPayment(balanceSheet, amount, date) => payment
    apply(balanceSheet, payment) => balanceSheet'
    newDiscount(balanceSheet', amount, date) => discount
    apply(balanceSheet', discount) => balanceSheet''
Applied to chess:

    newMovement(board, K, E2) => movement
    apply(board, movement) => board'
    newMovement(board', b, D6) => movement'
    apply(board', movement') => board''
As a bonus, it's clear where to enforce invariants and fail early:

    newMovement(board', K, E3) => Illegal Movement (Can't move to E3)
    newMovement(board'', K, E2) => Illegal Movement (White King not available on board)
    apply(board''', randomMovement) => Illegal Play





Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: