Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Using boolean logic to deal with dates

Using boolean logic to deal with dates

The unexpected application of Karnaugh maps in a Rails project

Irina Bednova

October 30, 2014
Tweet

Other Decks in Programming

Transcript

  1. For fun
    Using Boolean logic
    to deal with dates

    View full-size slide

  2. The problem
    Parse a string to a Date object

    View full-size slide

  3. The problem
    ● Date could be in the mm/dd/yyyy format

    View full-size slide

  4. The problem
    ● Date could be in the mm/dd/yyyy format
    ● Date could be in any format recognisable by
    Date.parse

    View full-size slide

  5. The problem
    ● Date could be in the mm/dd/yyyy format
    ● Date could be in any format recognisable by
    Date.parse
    ● Except the dd/mm/yyyy format

    View full-size slide

  6. month_day_year =
    Date.strptime(string, "%m/%d/%Y") rescue nil

    View full-size slide

  7. month_day_year =
    Date.strptime(string, "%m/%d/%Y") rescue nil
    generic_parse =
    Date.parse(string) rescue nil

    View full-size slide

  8. month_day_year =
    Date.strptime(string, "%m/%d/%Y") rescue nil
    generic_parse =
    Date.parse(string) rescue nil
    day_month_year =
    Date.strptime(string, "%d/%m/%Y") rescue nil

    View full-size slide

  9. month_day_year generic_parse day_month_year result
    0 0 0 0
    0 0 1
    0 1 0
    0 1 1
    1 0 0
    1 0 1
    1 1 0
    1 1 1

    View full-size slide

  10. month_day_year generic_parse day_month_year result
    0 0 0 0
    0 0 1 X
    0 1 0
    0 1 1
    1 0 0
    1 0 1 X
    1 1 0
    1 1 1

    View full-size slide

  11. month_day_year generic_parse day_month_year result
    0 0 0 0
    0 0 1 X
    0 1 0 1
    0 1 1
    1 0 0
    1 0 1 X
    1 1 0
    1 1 1

    View full-size slide

  12. month_day_year generic_parse day_month_year result
    0 0 0 0
    0 0 1 X
    0 1 0 1
    0 1 1 0
    1 0 0
    1 0 1 X
    1 1 0
    1 1 1

    View full-size slide

  13. month_day_year generic_parse day_month_year result
    0 0 0 0
    0 0 1 X
    0 1 0 1
    0 1 1 0
    1 0 0 1
    1 0 1 X
    1 1 0
    1 1 1

    View full-size slide

  14. month_day_year generic_parse day_month_year result
    0 0 0 0
    0 0 1 X
    0 1 0 1
    0 1 1 0
    1 0 0 1
    1 0 1 X
    1 1 0 X
    1 1 1

    View full-size slide

  15. month_day_year generic_parse day_month_year result
    0 0 0 0
    0 0 1 X
    0 1 0 1
    0 1 1 0
    1 0 0 1
    1 0 1 X
    1 1 0 X
    1 1 1 1

    View full-size slide

  16. Makes your boolean functions simpler!
    Karnaugh map

    View full-size slide

  17. 0 X 0 1
    1 X 1 X
    generic_parse * day_month_year
    month_day_year
    1 0
    00 01 11 10

    View full-size slide

  18. 0 X 0 1
    1 X 1 X
    generic_parse * day_month_year
    month_day_year
    1 0
    00 01 11 10

    View full-size slide

  19. 0 X 0 1
    1 X 1 X
    generic_parse * day_month_year
    month_day_year
    1 0
    00 01 11 10

    View full-size slide

  20. 0 X 0 1
    1 X 1 X
    generic_parse * day_month_year
    month_day_year
    1 0
    00 01 11 10

    View full-size slide

  21. month_day_year
    0 X 0 1
    1 X 1 X
    generic_parse * day_month_year
    month_day_year
    0
    00 01 11 10
    1

    View full-size slide

  22. month_day_year

    ¬generic_parse ∧
    day_month_year
    0 X 0 1
    1 X 1 X
    generic_parse * day_month_year
    month_day_year
    0
    00 01 11 10
    1

    View full-size slide

  23. month_day_year

    ¬generic_parse ∧
    day_month_year

    generic_parse ∧
    ¬day_month_year
    0 X 0 1
    1 X 1 X
    generic_parse * day_month_year
    month_day_year
    0
    00 01 11 10
    1

    View full-size slide

  24. ((month_day_year ? 1 : 0) +
    ((day_month_year ? 1 : 0) ^
    (generic_parse ? 1 : 0)) == 0)
    month_day_year || generic_parse

    View full-size slide

  25. Thank you!
    Irina Bednova
    @jafrog
    www.jafrog.com

    View full-size slide