June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
13
Task/Delegates/Clojure/delegates.clj
Normal file
13
Task/Delegates/Clojure/delegates.clj
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
(defprotocol Thing
|
||||
(thing [_]))
|
||||
|
||||
(defprotocol Operation
|
||||
(operation [_]))
|
||||
|
||||
(defrecord Delegator [delegate]
|
||||
Operation
|
||||
(operation [_] (try (thing delegate) (catch IllegalArgumentException e "default implementation"))))
|
||||
|
||||
(defrecord Delegate []
|
||||
Thing
|
||||
(thing [_] "delegate implementation"))
|
||||
15
Task/Delegates/Julia/delegates-1.julia
Normal file
15
Task/Delegates/Julia/delegates-1.julia
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
module Delegates
|
||||
|
||||
export Delegator, Delegate
|
||||
|
||||
struct Delegator{T}
|
||||
delegate::T
|
||||
end
|
||||
|
||||
struct Delegate end
|
||||
|
||||
operation(x::Delegator) = thing(x.delegate)
|
||||
thing(::Any) = "default implementation"
|
||||
thing(::Delegate) = "delegate implementation"
|
||||
|
||||
end # module Delegates
|
||||
11
Task/Delegates/Julia/delegates-2.julia
Normal file
11
Task/Delegates/Julia/delegates-2.julia
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
using .Delegates
|
||||
|
||||
a = Delegator(nothing)
|
||||
b = Delegator("string")
|
||||
|
||||
d = Delegate()
|
||||
c = Delegator(d)
|
||||
|
||||
@show Delegates.operation(a)
|
||||
@show Delegates.operation(b)
|
||||
@show Delegates.operation(c)
|
||||
25
Task/Delegates/Kotlin/delegates.kotlin
Normal file
25
Task/Delegates/Kotlin/delegates.kotlin
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
// version 1.1.51
|
||||
|
||||
interface Thingable {
|
||||
fun thing(): String?
|
||||
}
|
||||
|
||||
class Delegate(val responds: Boolean) : Thingable {
|
||||
override fun thing() = if (responds) "delegate implementation" else null
|
||||
}
|
||||
|
||||
class Delegator(d: Delegate) : Thingable by d {
|
||||
fun operation() = thing() ?: "default implementation"
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
// delegate doesn't respond to 'thing'
|
||||
val d = Delegate(false)
|
||||
val dd = Delegator(d)
|
||||
println(dd.operation())
|
||||
|
||||
// delegate responds to 'thing'
|
||||
val d2 = Delegate(true)
|
||||
val dd2 = Delegator(d2)
|
||||
println(dd2.operation())
|
||||
}
|
||||
25
Task/Delegates/Sidef/delegates.sidef
Normal file
25
Task/Delegates/Sidef/delegates.sidef
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
class NonDelegate { }
|
||||
|
||||
class Delegate {
|
||||
method thing {
|
||||
return "delegate implementation"
|
||||
}
|
||||
}
|
||||
|
||||
class Delegator (delegate = null) {
|
||||
method operation {
|
||||
|
||||
if (delegate.respond_to(:thing)) {
|
||||
return delegate.thing
|
||||
}
|
||||
|
||||
return "default implementation"
|
||||
}
|
||||
}
|
||||
|
||||
var d = Delegator()
|
||||
say "empty: #{d.operation}"
|
||||
d.delegate = NonDelegate()
|
||||
say "NonDelegate: #{d.operation}"
|
||||
d.delegate = Delegate()
|
||||
say "Delegate: #{d.operation}"
|
||||
Loading…
Add table
Add a link
Reference in a new issue