Giter VIP home page Giter VIP logo

romanumeric's Introduction

romanumeric

romanumeric is an enterprise-grade Roman numeral toy project inspired by the ROMAN() function from Excel.

It not only supports typical encoding and decoding, but also allows for creating and using custom numeral systems. The included Roman module is itself just a simple preset:

module Roman = struct
  let table =
    Table.make
      [ ('I', 1)
      ; ('V', 5)
      ; ('X', 10)
      ; ('L', 50)
      ; ('C', 100)
      ; ('D', 500)
      ; ('M', 1_000) ]

  let to_int = make_decoder table

  let of_int ?c:(compression_level = 0) =
    make_encoder table (compression_level + 1) 1
end

Usage

open Romanumeric

Decoding

# Roman.to_int "MCMXII"
- : decoding_result = Ok 1912

romanumeric follows a single rule of interpretation: if the code of a repetition has a lower value than the code of the next repetition, it is treated as negative (e.g. "XIIV" is interpreted as "10 + (-2) + 5").

Because of this, there is no problem decoding "non-standard" numerals like the following examples from history:

let decode n = Result.get_ok (Roman.to_int n);;

assert (decode "IIIXX" = 17);;
assert (decode "IIXX" = 18);;
assert (decode "IIIC" = 97);;
assert (decode "IIC" = 98);;
assert (decode "IC" = 99);;
assert (decode "IIX" = 8);;
assert (decode "XIIX" = 18);;
assert (decode "XXIIX" = 28);;

Compare this to the output from Google Sheets:

Input Expected Actual
=ARABIC("IIIXX") 17 #VALUE!
=ARABIC("IIXX") 18 #VALUE!
=ARABIC("IIIC") 97 #VALUE!
=ARABIC("IIC") 98 #VALUE!
=ARABIC("IC") 99 99
=ARABIC("IIX") 8 #VALUE!
=ARABIC("XIIX") 18 #VALUE!
=ARABIC("XXIIX") 28 #VALUE!

A historical example of a truly non-standard numeral would be the use of "IIXX" to indicate 22 (as "two and twenty"). Under the standard rules of interpretation, "IIXX" evaluates to 18:

# Roman.to_int "IIXX"
- : decoding_result = Ok 18

Encoding

# Roman.of_int 1234
- : encoding_result = Ok "MCCXXXIV"

Compression

Like ROMAN(), compressed encoding is supported:

# Roman.of_int ~c:0 499
- : encoding_result = Ok "CDXCIX"
# Roman.of_int ~c:1 499
- : encoding_result = Ok "LDVLIV"
# Roman.of_int ~c:4 499
- : encoding_result = Ok "ID"

Limitations

Unlike ROMAN(), input is not restricted to the arbitrary 1-3999 range:

# Roman.of_int 0
- : encoding_result = Ok ""
# Roman.of_int 5348
- : encoding_result = Ok "MMMMMCCCXLVIII"

Negative numbers are still not supported, however:

# Roman.of_int (-5)
- : encoding_result = Error "Negative numbers are not supported"

romanumeric's People

Contributors

nsmmrs avatar

Stargazers

 avatar  avatar

Watchers

 avatar

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.