Question: 1 ) package functional import functional.data. { Graph , Tree } trait Foldable [ F [ _ ] ] : def fold [ A ]
package functional
import functional.data.Graph Tree
trait FoldableF:
def foldAt: FAm: MonoidA: A
def foldMapA Bt: FAf: A Bm: MonoidB: B
object ListFoldable extends FoldableList:
override def foldAt: ListAm: MonoidA: A t match
case Nil mempty
case hd :: tl mcombinehd foldtlm
override def foldMapA Bt: ListAf: A Bm: MonoidB: B
object TreeFoldable TODO
object GraphFoldable TODO
def wordCountFt: FStringf: FoldableF: Int
def charCountFt: FStringf: FoldableF: Int
a Implement foldables points
Task: Complete the implementations of the foldables in functional.Foldable. Extend the given objects so that they inherit from Foldable and implement the necessary methods.
The foldables for the data structures List, Tree and Graph need to be implemented.
Important: The given interfaces do not use any implicit parameters. You need to find the appropriate monoids explicitly
passed as a parameter.
Hints:
Two graphs can be merged using the monoid GraphMerge see task
The neighboring nodes of a graph can be edited using ListFoldable.
b Apply foldables points
Task: Implement the two methods wordCount and charCount in functional.Foldable. The method
wordCount is intended to accumulate the number of words in the strings contained within the foldable data structure. Assume all words are separated by a space.
Example: wordCountListhello world", "foo bar"ListFoldable
charCount should return the total number of characters in the strings contained within the foldable data structure.
Example: charCountListhello world", "foo bar"ListFoldable
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
