score:5
you can get some of the behavior you're after with the switch
function. however, r does not have sum types like the ones you're used to in scala and f#.
r is a dynamic language, a lot like scheme and lisp but with different syntax. you're going to have to make a few adjustments if you're coming from a strongly typed language like f# or scala. you lose the benefits of strong types, but you gain the benefits of dynamic ones.
it turns out that for data analysis, dynamic typing is often a better fit.
score:1
r is... very different. to do something like that, you would use s4 generics and method dispatch, which use a signature to access methods. it would essentially reproduce the behavior inside your matcher object. here is the sample code:
#- typical r oo code uses the concept of generic function dispatch. to use "work" as a
#- method, i need to set the generic function, from which all other methods will be
#- dispatched from. normally, r does this by default, but it is always nicer to declare it
#- directly, as it shows that you *own* said generic. this is the example for s4 methods,
#- so the dispatching might look weird at first (?).
require("methods")
setgeneric("work", function(one, two) print("default"))
#- now that we have a generic, we can create your classes and use method
#- dispatch to produce a similar effect to your pattern matcher.
setclass("a")
setclass("b", contains = "a")
setclass("c", contains = "a")
# now, the equivalent for your "cases" in r...
setmethod("work", c(one = "b", two = "b"), function(one, two) print("b"))
setmethod("work", c(one = "b", two = "c"), function(one, two) print("c"))
work(new("b"), new("b"))
# b
work(new("b"), new("c"))
# c
#- just an additional print call to make it obvious
work()
# default
note that i could have just as well defined it for independent classes "b" and "c", and it would have worked. secondly, encapsulating this code inside an object as you did would be very tedious (e.g. using r6/reference classes or something like that), and highlights the difference with r. functions just love to live outside objects in r, and so it is easier to dispatch them with the object you intend to use them in as an additional argument for the signature, rather than placing them inside a particular object structure.
Source: stackoverflow.com
Related Query
- Is there any language construct in scala that can assure all cases of the pattern matching have been handled?
- As a functional programming language, does R have pattern matching? (As in Scala, or F#)
- Why does pattern matching in Scala not work with variables?
- How does Pattern Matching in Scala overcome duplication that switch case causes?
- why does filter have to be defined for pattern matching in a for loop in scala?
- If Singletons are so bad, why does Scala have language support for them?
- Does it make any sense to use pattern matching in Scala with really simple cases?
- How does Scala Cons pattern matching determine the head and the tail of a List?
- Does scala have shortcuts for functional objects?
- Why does this snippet with pattern matching and higher kinded types no longer compile in Scala 2.12?
- Why does Scala not infer the type parameters when pattern matching with @
- According to the Scala Language Specification, packages are AnyRef values and have types. How does this make sense?
- Why does Scala regexp work differently in pattern matching
- pattern matching in scala works inside a def, otherwise does not
- Why does Regex pattern matching not work sometimes in Scala
- scala and akka function literal ? need help in understanding the language of functional pattern
- Iterating or Pattern Matching a element that may have inner lists in Scala
- To which language is Scala most similar from a functional programming perspective?
- Scala - pattern matching traits that have "self type" as type argument gives unchecked warning
- why closures are part of scala or any functional programming language
- How is pattern matching in Scala implemented at the bytecode level?
- Difference between reduce and foldLeft/fold in functional programming (particularly Scala and Scala APIs)?
- Scala multiple type pattern matching
- Scala pattern matching on sequences other than Lists
- Functional Reactive Programming in Scala
- Does Scala have guards?
- Is Scala functional programming slower than traditional coding?
- Does Scala have a library method to build Option-s that takes into account empty strings?
- Why does Scala choose to have the types after the variable names?
- How does | (pipe) in pattern matching work?
More Query from same tag
- How can I print a list which is in another list? (scala)
- Hadoop s3 configuration file missing
- concatenating maptype values while doing groupby on dataframe
- AggregateByKey fails to compile when it is in an abstract class
- Concat sorted generic sequence
- java.lang.NoSuchMethodError in Scala SBT Shell after executing JAR
- Gatling. What would be the best way to pass feeder data to other functions?
- Constructor signature is changed if I define case class in spark-shell (scala-shell)
- csv file read and loop based on filter
- Is there a way to change the replication factor of RDDs in Spark?
- InvalidGroupIdException: in Kafka scala consumer program even after setting group.id
- Scala HashMap containsValue method
- Generate wildcard binding pattern with macros
- Functional scala log accumulator
- spark scala avro write fails with AbstractMethodError
- Slick Connection Pool on per requests
- create pyspark dataframes with names from list
- Scala Range contains(elem: Any) method
- how to efficiently build up complex case class in my app flow?
- Pattern matching without case classes
- IncompatibleClassChangeError in Scala Spring-Boot application
- Error when use akka http unmarshall entity as case class with default value
- Do cats and scalaz create performance overhead on application?
- ant does not expand my scala home directory
- Print the JSON response from GET request in Finagle
- ClassTag.runtimeClass.isInstance does not work for AnyVal
- Eclipse jobs not in Spark UI History
- How to declare a new partial function type in Scala
- Is blocking within a future still blocking?
- Can't open working VIM editor from Java/Scala console application