首页 \ 问答 \ Scalaz Reader to ReaderT(Scalaz Reader to ReaderT)

Scalaz Reader to ReaderT(Scalaz Reader to ReaderT)

我无法更改的函数返回Scalaz Reader

type Action[A] = Reader[Session, A]

def findAccount(s: String): Action[Account] = 
  Reader((session: Session) => Account(s))

我想基于findAccount(...)创建一个新函数来返回ReaderT[Option, Session, A]

type ActionT[A] = ReaderT[Option, Session, A]

def findAccountT(s: String): ActionT[Account] = findAccount(s).map(Option(_))

因为最终我想这样做,

def findAccBalT(accountNumber: String) = for {
  acc <- findAccountT(accountNumber)
  bal <- findBalanceT(acc)
}  yield bal

我该怎么办? 是否有意义? 谢谢

全面披露,

import scalaz._
import Scalaz._        

trait Session {
  def doSomething(): Unit
}

case class Account(number: String) extends AnyVal
case class Amount(value: Int, currency: String) 
case class DBSession() extends Session {
  override def doSomething = println("writing to db")
}

type Action[A] = Reader[Session, A]
type ActionT[A] = ReaderT[Option, Session, A]

def findAccount(s: String): Action[Account] = 
  Reader((session: Session) => Account(s))

def findBalance(account: Account): Action[Amount] = 
  Reader((session: Session) => Amount(333, "S$"))

// failed
def findAccountT(s: String): ActionT[Account] = findAccount(s).map(Option(_))

// failed
def findBalanceT(account: Account): ActionT[Amount] = findBalance(account).map(Option(_))

// failed
def findAccBalT(accountNumber: String) = for {
  acc <- findAccountT(accountNumber)
  bal <- findBalanceT(acc)
}  yield bal

A function which I cannot change returns Scalaz Reader,

type Action[A] = Reader[Session, A]

def findAccount(s: String): Action[Account] = 
  Reader((session: Session) => Account(s))

I want create a new function based on findAccount(...) to return ReaderT[Option, Session, A] as in

type ActionT[A] = ReaderT[Option, Session, A]

def findAccountT(s: String): ActionT[Account] = findAccount(s).map(Option(_))

because eventually I want to do this,

def findAccBalT(accountNumber: String) = for {
  acc <- findAccountT(accountNumber)
  bal <- findBalanceT(acc)
}  yield bal

How do I proceed? Does it make sense? Thanks

Full disclosure,

import scalaz._
import Scalaz._        

trait Session {
  def doSomething(): Unit
}

case class Account(number: String) extends AnyVal
case class Amount(value: Int, currency: String) 
case class DBSession() extends Session {
  override def doSomething = println("writing to db")
}

type Action[A] = Reader[Session, A]
type ActionT[A] = ReaderT[Option, Session, A]

def findAccount(s: String): Action[Account] = 
  Reader((session: Session) => Account(s))

def findBalance(account: Account): Action[Amount] = 
  Reader((session: Session) => Amount(333, "S$"))

// failed
def findAccountT(s: String): ActionT[Account] = findAccount(s).map(Option(_))

// failed
def findBalanceT(account: Account): ActionT[Amount] = findBalance(account).map(Option(_))

// failed
def findAccBalT(accountNumber: String) = for {
  acc <- findAccountT(accountNumber)
  bal <- findBalanceT(acc)
}  yield bal

原文:https://stackoverflow.com/questions/37641796
更新时间:2023-12-06 19:12

最满意答案

正如Santi所说,它没有自己的,但是一些可用于SQLAlchemy的应该可以工作:

sqlalchemy-migrate也被tesla-pylons-elixir用作与Pylons更好整合的一种方式。

miruku


As Santi said, it doesn't have its own, but some of the ones available for SQLAlchemy should work:

sqlalchemy-migrate which is also used by tesla-pylons-elixir as a way to get better integration with Pylons.

miruku

相关问答

更多
  • South 1.0提供了解决方案。 它将首先在south_migrations/文件夹中查找,并回south_migrations/ migrations/ 。 因此,对于需要支持较老和较新的Djangos的第三方库,请将South文件移动到south_migration/并在migrations/创建新的1.7迁移。 South 1.0 发行说明 Django文档也添加了这些信息 South不能与Django 1.7一起使用,但这对于最终用户来说不是问题。 他们要么使用新的Django,要么使用旧的Dja ...
  • 为了将来参考,这是我最终这样做的方式: if args.settings_dir not in sys.path: sys.path.append(args.settings_dir) os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' #try to query db for existing objects, e.g. user groups #in order to detect if we are upgrading an existing i ...
  • 正如Santi所说,它没有自己的,但是一些可用于SQLAlchemy的应该可以工作: sqlalchemy-migrate也被tesla-pylons-elixir用作与Pylons更好整合的一种方式。 miruku As Santi said, it doesn't have its own, but some of the ones available for SQLAlchemy should work: sqlalchemy-migrate which is also used by tesla-p ...
  • 扁平化迁移意味着基本上只是将所有迁移分块,以便在迁移变得相当大时减少迁移的运行时间。 这是可能的,但不推荐,因为您将有效地丢失所有迁移历史记录,使您运行的每个迁移从0开始。 我没有读过“推荐”扁平化的任何地方所以我不能给你那条建议,如果你有一个链接,我会很乐意阅读它。 我已经读过Ruby on Rails有这个功能,但也不建议这样做。 首先考虑这些问题: 这是你真正想要的吗? 你会失去所有的历史。 这将导致迁移到版本0然后再次备份,这将经常进行 如果这样做是为了阻止我将使用的任何丢失或无序迁移 python ...
  • 嗯......答案是使用应用程序。 这就是他们的目的。 它们的设计方式正是因为标准模块不能提供所需的集成水平。 如果你开始乱砍你的库以使它自己工作,你最终会得到大量的代码和胶水相同大小的django app,但气味要差得多。 Well... the answer is to use apps. That's what they're for. They were designed the way the are exactly because standard modules don't provide t ...
  • 您有db/schema.rb文件,其中表示每个模型的所有字段(以一次大迁移的形式)。 数据(字段)和行为(方法)之间的分离是有意的,它可以使用库来覆盖,这些库使用头部注释中最新适用的模式的副本来注释模型 You have the db/schema.rb file, where all the fields of each model are expressed (in the form of one big migration). The separation between data (fields) ...
  • 为什么在syncdb之后进行迁移? 当然,在新创建的数据库之后你没有迁移吗? 另请注意南方设置: SKIP_SOUTH_TESTS = True SOUTH_TESTS_MIGRATE = False Why do a migrate after a syncdb? Surely you have no migration to do after a freshly created db? Also note the south settings: SKIP_SOUTH_TESTS = True SOUTH ...
  • 您应该能够将表单传递给模板,在模板本身内添加任何自定义CSS规则的同时将其呈现在那里。 例如,在模板头部使用