truizlop on utilities
truizlop on master
Miscellaneous utilities (#459) … (compare)
truizlop on utilities
Add docs (compare)
truizlop on utilities
Fix wrong documentation Add instances of Semigroup and … Add utility to Kleisli and 2 more (compare)
miguelangel-dev on calvellido-patch-1
miguelangel-dev on master
Fix bad space char at docs (#45… (compare)
Functor
for Co
. As per its definition, we can only change A
to B
, but in this case we would also need to transform the existential witness. I am afraid the current encoding we have to simulate HKTs does not allow to do this, or at least I cannot think of a way to do it at the moment.
Reader<D, A>
is like a plain function (D) -> A
ask
to get a Reader<D, D>
IO
:
protocol Storage {
func save(_ user: User) -> IO<Never, Bool>
}
protocol API {
func fetch(byId: Int) -> IO<Never, User>
}
struct Env {
let database: Storage
let api: API
}
func cacheUser(byId userId: Int) -> ReaderT<IOPartial<Never>, Env, Bool>
which means: "Give me a user ID, and I will return a Reader that, when provided an Env
will have all it needs to produce IO<Never, Bool>
"
Env
for production, or another one for testing:
let result = try! cacheUser(byId: 1234)
.invoke(Env(database: StorageImpl(),
api: APIImpl()))^
.unsafePerformIO()
Hi @truizlop , I was playing around with Bow_Swift and trying to figure out where to use which effect. I have a particular scenario as follows:-
struct User {
let id: String
let name: String
}
protocol Storage {
func get(_ id: String) -> IO<Never, User>
func save(_ user: User) -> IO<Never, Bool>
}
protocol API {
func fetch(byId: Int) -> IO<Never, User>
}
struct UserUsecase {
func getUser(byID id: String) -> What kind of effect can I use here? {
// Get the data from storage
// return the cached data
// fetch the data from API
// save or update the user data from API response
// return back the latest user info
}
}
The above function getUserbyID first gets the user data from cache returns that value and in parallel fetched data from API and updates cache and returns back the updates value.
This particular scenario comes a lot in Mobile client application where we cache and update the data from external source.
So What kind of Effect or return type to use here. As any return type can only return once. The only way I can think of is to use Rx Stream. Do we have any other solution or effect to achieve this in a better way?