假设您需要连接到数据库。
所以,你给
DbConnection
作为某个具有如下类型的假设函数的最后一个参数:
doDbStuff :: Int -> DbConnection -> Int
也许还有其他的功能也依赖于
数据库连接类
交易
因为一个人可能想管理
数据库连接类
使用
水塘
从池和到池的实例。
现在,这些函数是一个长函数组合的一部分,某些决策可能涉及不需要
. 也就是说,有可能
可能从池中获取,并且可能被另一个请求使用,这可能会产生瓶颈。
还有一种选择,就是不注射
但是一个高阶函数
withConnection :: (DbConnection -> a) -> a
数据库连接类
,使用它和整个
withConnection
所以。。。
JavaScript中的伪代码:
const connectionString = '[whatever]'
const env = { connection: acquire (connectionString) }
const output = composition (arg0) (argN) (env)
const f = x => y => ({ connection }) =>
doDbStuff (x + y) (connection)
方法2
const withConnection = f => [stuff to acquire the connection, and aftewards, release it]
const env = { withConnection }
const output = composition (arg0) (argN) (env)
const f = x => y => ({ withConnection }) =>
withConnection (doDbStuff (x + y))