如何使用Reasonml / Bucklescript将字符串解析为整数?

时间:2017-12-31 19:24:18

标签: reason bucklescript

我正在学习Reasonml,我无法在标准库中找到任何函数,而Bucklescript Js模块也没有。有没有比使用原始javascript 更好的选择?

现在我用这个功能实现它:

import tkinter as tk
import numpy
import math

def fishers():
    cell_a = input("Enter Cell A value: ")
    cell_b = input("Enter Cell B value: ")
    cell_c = input("Enter Cell C value: ")
    cell_d = input("Enter Cell D value: ")
cell_a = float(cell_a)
cell_b = float(cell_b)
cell_c = float(cell_c)
cell_d = float(cell_d)
fenum = math.factorial(cell_a+cell_b)*math.factorial(cell_c+cell_d)*
math.factorial(cell_a+cell_c)*math.factorial(cell_b+cell_d)
feden = 
math.factorial(cell_a)*math.factorial(cell_b)*math.factorial(cell_c)*
math.factorial(cell_d)*math.factorial(cell_a+cell_b+cell_c+cell_d)
fe = fenum/feden
print ("Fishers Exact: =\t {:>8.4f}\n".format(fe))
#This is test code to see if I can write into the canvas        
self.fishers.canvas.create_text(100,10,fill="darkblue",font="Times 20 
italic bold", text="We will enter the data here.")

class Window(tk.Frame):

 def __init__(self, master=None):
    super().__init__(master)
    self.master = master
    self.main_window()
    self.widgets()

# All main window parameters go here
 def main_window(self):
    self.master.title("Quick-Statistics Calculator")
    self.master.geometry("1000x800")
    self.pack()

 def widgets(self):
    main_menu = tk.Menu(self.master, tearoff=0)
    self.master.config(menu=main_menu)

    # Create File Menu and Drop Down
    file = tk.Menu(main_menu, tearoff=0)
    file.add_command(label="Save")
    file.add_separator()
    file.add_command(label="Exit", command=exit)
    main_menu.add_cascade(label="File", menu=file)

    # Create Edit Menu and Drop Down
    edit = tk.Menu(main_menu, tearoff=0)
    edit.add_command(label="Undo")
    main_menu.add_cascade(label="Edit", menu=edit)

    #Create Means Analysis Menu and Drop Down
    means = tk.Menu(main_menu,tearoff=0)
    means.add_command(label="Group t")
    means.add_command(label="Paired t")
    means.add_separator()
    means.add_command(label="ANOVA")
    means.add_separator()
    means.add_command(label="Mann-Whitney U")
    means.add_command(label="Wilcoxon rank sum")
    main_menu.add_cascade(label="Means", menu=means)

    #Create Chi-square Analysis Menus and Drop Down
    chi = tk.Menu(main_menu, tearoff=0)
    chi.add_command(label="Chi-Square 2x2 table")
    chi.add_command(label="Chi-Square for larger table")
    chi.add_separator()
    chi.add_command(label="McNemar's Test")
    chi.add_command(label="Fisher's Exact", command=fishers)
    main_menu.add_cascade(label="Tables", menu=chi)

    #Create Help Menu
    help = tk.Menu(main_menu, tearoff=0)
    help.add_command(label="Means Analysis")
    help.add_command(label="Chi-Square Analysis")
    main_menu.add_cascade(label="Help",menu=help)

main = tk.Tk()
comm_prog = Window(master=main)
main.mainloop()

1 个答案:

答案 0 :(得分:8)

int_of_string(以及float_of_string / bool_of_string)应该做你需要的。

它在标准库中,您应该能够搜索它https://caml.inria.fr/pub/docs/manual-ocaml/libref/Pervasives.html(如果您安装了reason-tools浏览器扩展程序,该网站将更适合您,因此它会自动从OCaml转换为你的理由语法)

请注意,如果字符串对该类型无效,则所有这些函数都将抛出异常(请阅读链接以了解每个函数的工作原理以及每个字符串对字符串的期望)。

正如@glennsl指出的那样,当Bucklescript赶上OCaml编译器的最新版本而不是4.02.3时,你可能想要使用更安全的_opt变体,例如int_of_string_opt会返回Some(number)None,具体取决于您对输入的信任程度,您希望避免多少异常,以及您希望如何处理错误输入(是例外,应该杀死程序/堆栈,还是正常,应该在本地处理?)。