-
-
Save Fingerzam/23443cad597aeb0d52aab5f03f98d738 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait ExtractableFields { | |
val name: Option[String] | |
val color: Option[String] | |
} | |
case class Event( | |
id: Integer, | |
etype: String, | |
name: Option[String], | |
color: Option[String], | |
) extends ExtractableFields | |
case class AggregatedData( | |
name: Option[String], | |
color: Option[String], | |
) extends ExtractableFields | |
val nullAD = SessionData(None, None, None) | |
def mergeFields(agg: ExtractableFields, event: ExtractableFields): AggregatedData = { | |
// move each field's value defined in ExtractableFields trait to aggregate-object | |
// if there is not already a value | |
} | |
def flattenData(events: List[ExtractableFields]): AggregatedData = { | |
events.foldLeft(nullAD)((agg, event) => { | |
mergeFields(agg, event) | |
}) | |
} | |
// val a = Event(1, "UP", None, None) | |
// val b = Event(2, "DOWN", Some("Tim"), None) | |
// val c = Event(3, "LEFT", None, "Green") | |
// val d = Event(4, "UP", None, "Blue") | |
//flattenData(List(a, b, c)) | |
// -> | |
// AggregatedData("Tim", "green") | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(def events | |
[{:id 1 :event "UP" :name nil :color nil} | |
{:id 2 :event "DOWN" :name "Tim" :color nil} | |
{:id 3 :event "LEFT" :name nil :color "Green"} | |
{:id 4 :event "UP" :name nil :color "Blue"}]) | |
(def null-ad {:name nil :color nil}) | |
(def fields [:name :color]) | |
(defn merge-fields [agg event] | |
(println agg event) | |
(merge-with #(or %1 %2) agg (select-keys event fields))) | |
(defn flatten-data [events] | |
(reduce merge-fields null-ad events)) | |
(flatten-data events) | |
;=> {:color "Green", :name "Tim"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(def ExtractableFields | |
{:name s/Str | |
:color s/Int}) | |
(def EventTypeful | |
{:etype s/Str}) | |
(def Event | |
(merge | |
{:id s/Int} | |
EventTypeful | |
ExtractableFields)) | |
(def AggregatedData | |
ExtractableFields) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment