定义和使用全局变量的最简单方法
Easiest way of defining and using of Global Variable
"first part" &&&& fun _ ->
let ident
"second part" &&&& fun _ ->
ident ....
我需要使用变量 "ident"。
我只需要将变量值从测试的第一部分传递到第二部分...
我想问你是否有任何简单的方法来定义和使用全局变量,或者即使你有更好(更简单)的想法
请记住,我是初学者,所以我更喜欢简单的。
如果您像这样在文件顶部定义 ident
:
let ident = "foo"
// rest of your code using ident
ident
是全局的,您可以在文件的下一部分中使用。
编辑:
如果 ident
将在您的代码的下一部分中更改,请使用此 :
let ident = ref "foo"
由于评论已经失控,这就是我举个例子的方法
let mutable t = 0
let first =
t <- 1 + 1
//other stuff
let second =
//can use t here and it will have a value of 2
在某些情况下,您必须使用参考:
let t = ref 0
let first =
t := 1 + 1
//other stuff
let second =
//can use t here and it will have a value of 2 -
// you use "!t" to get the value
全局变量通常会使您的代码难以使用 - 特别是如果它们是可变的。
相反,考虑将您需要跟踪的值作为复合值返回。一个简单的数据类型是元组:
let ``first part`` id =
let someOtherValue = "Foo"
someOtherValue, id + 1
此函数以 int
(当前 ID)作为输入,returns string * int
(第一个元素为 string
的元组,以及第二个元素和 int
) 作为输出。
你可以这样称呼它:
> let other, newId = ``first part`` 42;;
val other : string = "Foo"
val newId : int = 43
请注意,您可以使用模式匹配立即将值解构为两个命名符号:other
和 newId
。
您的第二个函数也可以将 ID 作为输入:
let ``second part`` id otherArgument =
// use id here, if you need it
"Bar"
你可以这样称呼它,使用上面的 newId
值:
> let result = ``second part`` newId "Baz";;
val result : string = "Bar"
如果您发现自己经常这样做,您可以为此目的定义一条记录:
type Identifiable<'a> = { Id : int; Value : 'a }
现在您可以开始定义高阶函数来处理这种类型,例如map
函数:
module Identifiable =
let map f x = { Id = x.Id; Value = f x.Value }
// Other functions go here...
这是一个将 Identifiable 的 Value
从一个值映射到另一个值的函数,但保留了身份。
下面是一个简单的使用示例:
> let original = { Id = 42; Value = "1337" };;
val original : Identifiable<string> = {Id = 42;
Value = "1337";}
> let result' = original |> Identifiable.map System.Int32.Parse;;
val result' : Identifiable<int> = {Id = 42;
Value = 1337;}
如您所见,它保留了值 42
,但将 Value
从 string
更改为 int
。
您仍然可以明确更改 ID,如果您想这样做的话:
> let result'' = { result' with Id = 7 };;
val result'' : Identifiable<int> = {Id = 7;
Value = 1337;}
"first part" &&&& fun _ ->
let ident
"second part" &&&& fun _ ->
ident ....
我需要使用变量 "ident"。
我只需要将变量值从测试的第一部分传递到第二部分...
我想问你是否有任何简单的方法来定义和使用全局变量,或者即使你有更好(更简单)的想法
请记住,我是初学者,所以我更喜欢简单的。
如果您像这样在文件顶部定义 ident
:
let ident = "foo"
// rest of your code using ident
ident
是全局的,您可以在文件的下一部分中使用。
编辑:
如果 ident
将在您的代码的下一部分中更改,请使用此 :
let ident = ref "foo"
由于评论已经失控,这就是我举个例子的方法
let mutable t = 0
let first =
t <- 1 + 1
//other stuff
let second =
//can use t here and it will have a value of 2
在某些情况下,您必须使用参考:
let t = ref 0
let first =
t := 1 + 1
//other stuff
let second =
//can use t here and it will have a value of 2 -
// you use "!t" to get the value
全局变量通常会使您的代码难以使用 - 特别是如果它们是可变的。
相反,考虑将您需要跟踪的值作为复合值返回。一个简单的数据类型是元组:
let ``first part`` id =
let someOtherValue = "Foo"
someOtherValue, id + 1
此函数以 int
(当前 ID)作为输入,returns string * int
(第一个元素为 string
的元组,以及第二个元素和 int
) 作为输出。
你可以这样称呼它:
> let other, newId = ``first part`` 42;;
val other : string = "Foo"
val newId : int = 43
请注意,您可以使用模式匹配立即将值解构为两个命名符号:other
和 newId
。
您的第二个函数也可以将 ID 作为输入:
let ``second part`` id otherArgument =
// use id here, if you need it
"Bar"
你可以这样称呼它,使用上面的 newId
值:
> let result = ``second part`` newId "Baz";;
val result : string = "Bar"
如果您发现自己经常这样做,您可以为此目的定义一条记录:
type Identifiable<'a> = { Id : int; Value : 'a }
现在您可以开始定义高阶函数来处理这种类型,例如map
函数:
module Identifiable =
let map f x = { Id = x.Id; Value = f x.Value }
// Other functions go here...
这是一个将 Identifiable 的 Value
从一个值映射到另一个值的函数,但保留了身份。
下面是一个简单的使用示例:
> let original = { Id = 42; Value = "1337" };;
val original : Identifiable<string> = {Id = 42;
Value = "1337";}
> let result' = original |> Identifiable.map System.Int32.Parse;;
val result' : Identifiable<int> = {Id = 42;
Value = 1337;}
如您所见,它保留了值 42
,但将 Value
从 string
更改为 int
。
您仍然可以明确更改 ID,如果您想这样做的话:
> let result'' = { result' with Id = 7 };;
val result'' : Identifiable<int> = {Id = 7;
Value = 1337;}