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

Practical Functional Programming in Ruby

Lukas Rieder
November 07, 2013

Practical Functional Programming in Ruby

This lightning talk presentation shows off how you can build a system of Classifiers to detect cheaters. All in a functional way, very modular, readable and testable.

Held at the Berlin Ruby User Group on 7 Nov 2013.

Lukas Rieder

November 07, 2013
Tweet

More Decks by Lukas Rieder

Other Decks in Technology

Transcript

  1. ~ NDA ~ ! If you meet Diego Echeverri, please

    praise him for the code he has written. By the way, don’t show this code to anyone else, it is not my code. __________________________________ (Date, Signature) creator of awesome things Ruby
  2. ScoreCheatClassifier class ScoreCheatClassifier < CheaterClassifier SCORE_FILTER_MAX = 2_000_000 SCORE_FILTER_MIN =

    1_400_000 ! def initialize(last_score) @last_score = last_score end ! def cheater? if @last_score > SCORE_FILTER_MAX :yes elsif @last_score < SCORE_FILTER_MIN :no else :maybe end end end
  3. LevelClassifier class LevelClassifier < CheaterClassifier BASE_LEVEL = 50 BASE_SCORE =

    1_700_000 ! def initialize(score, level) @score = score @level = level end ! def cheater? if @score > BASE_SCORE :yes else @level >= BASE_LEVEL ? :maybe : :yes end end end
  4. PlusClassifier class PlusClassifier < CheaterClassifier def initialize(a, b) @a =

    a @b = b end ! def cheater? partial_result = @a.cheater? if partial_result == :maybe @b.cheater? else partial_result end end end
  5. MainClassifier (Entry Point) class MainClassifier < CheaterClassifier def initialize(last_score, level)

    classifiers = [ ScoreCheatClassifier.new(last_score), LevelClassifier.new(last_score, level) ] @classifier = classifiers.reduce { |a,b| a + b } end end