Using boolean logic to deal with dates

Using boolean logic to deal with dates

The unexpected application of Karnaugh maps in a Rails project

E0f3a12b1ce60c13f52fd723152d810a?s=128

Irina Bednova

October 30, 2014
Tweet

Transcript

  1. For fun Using Boolean logic to deal with dates

  2. The problem

  3. The problem Parse a string to a Date object

  4. The problem • Date could be in the mm/dd/yyyy format

  5. The problem • Date could be in the mm/dd/yyyy format

    • Date could be in any format recognisable by Date.parse
  6. 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
  7. month_day_year = Date.strptime(string, "%m/%d/%Y") rescue nil

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

    nil
  9. 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
  10. 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
  11. 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
  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 1 0 0 1 0 1 X 1 1 0 1 1 1
  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 0 1 X 1 1 0 1 1 1
  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 1 1 1
  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
  16. 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
  17. Makes your boolean functions simpler! Karnaugh map

  18. 0 X 0 1 1 X 1 X generic_parse *

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

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

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

    day_month_year month_day_year 1 0 00 01 11 10
  22. 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
  23. 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
  24. 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
  25. ((month_day_year ? 1 : 0) + ((day_month_year ? 1 :

    0) ^ (generic_parse ? 1 : 0)) == 0) month_day_year || generic_parse
  26. Thank you! Irina Bednova @jafrog www.jafrog.com