我"fp-ts": "^2.10.5"在我的打字稿/反应项目中使用,并且收到一条警告,称“管道”已被弃用。下面的代码来自本教程,介绍如何使用 fp-ts 进行错误处理和验证:
import { Either, left, right } from 'fp-ts/lib/Either'
import { chain } from 'fp-ts/lib/Either'
import { pipe } from 'fp-ts/lib/pipeable'
//
const minLength = (s: string): Either<string, string> =>
s.length >= 6 ? right(s) : left('at least 6 characters')
const oneCapital = (s: string): Either<string, string> =>
/[A-Z]/g.test(s) ? right(s) : left('at least one capital letter')
const oneNumber = (s: string): Either<string, string> =>
/[0-9]/g.test(s) ? right(s) : left('at least one number') …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 fp-ts 实现一些简单的数据验证,并遇到了这个codesandboxexample:
import * as E from "fp-ts/lib/Either";
import { getSemigroup, NonEmptyArray } from "fp-ts/lib/NonEmptyArray";
import { sequence } from "fp-ts/lib/Array";
import { pipe } from "fp-ts/lib/pipeable";
import { Predicate } from "fp-ts/lib/function";
type ValidationError = NonEmptyArray<string>;
type ValidationResult = E.Either<ValidationError, unknown>;
type ValidationsResult<T> = E.Either<ValidationError, T>;
interface Validator {
(x: unknown): ValidationResult;
}
interface Name extends String {}
const applicativeV = E.getValidation(getSemigroup<string>());
const validateName: (
validations: Array<Validator>,
value: unknown
) => ValidationsResult<Name> = (validations, value) => …Run Code Online (Sandbox Code Playgroud) 一段时间以来,我一直在使用公共谷歌电子表格作为我的几个网络项目的 JSON 端点。我喜欢这种方法,因为我可以从公共电子表格中检索数据,而不需要任何类型的身份验证或令牌来获取数据,我所需要做的就是发布电子表格,然后从一个简单的 URL 获取:
fetch(`https://spreadsheets.google.com/feeds/${mode}/${id}/${sheetNum}/public/values?alt=json`).then((res)=>console.log(res));
Run Code Online (Sandbox Code Playgroud)
Google 正在弃用 Sheets v3,我对如何迁移到 v4 感到困惑。从这个答案 我收集到我需要提供通过谷歌云控制台创建的访问令牌。但我是否需要创建某种特殊的“应用程序”或“工作场所”,或者任何旧的 API 令牌都可以吗?我尝试了以下方法:
fetch(https://sheets.googleapis.com/v4/spreadsheets/SPREADSHEET_ID/values/RANGE?key=API_KEY)
Run Code Online (Sandbox Code Playgroud)
但这给了我 403 响应。
当我的组件中的某些道具在成帧器运动中发生变化时,我想淡出该属性,然后淡入“新”属性值?
这是我想象的动画时间线:
但我认为使用成帧器做到这一点的唯一方法是使用超时。除了使用超时之外还有其他方法可以达到这种效果吗?
import { motion } from "framer-motion";
import { useEffect, useState } from "react";
import "./styles.css";
export default function App() {
const [seconds, setSeconds] = useState(0);
const [anim, setAnim] = useState("in");
useEffect(() => {
const interval = setInterval(() => {
setSeconds((seconds) => seconds + 1);
setAnim("in");
setTimeout(() => {
setAnim("out");
}, 500);
}, 1000);
return () => clearInterval(interval);
}, []);
const variants = {
out: {
opacity: 0
},
in: {
opacity: 1,
transition: …Run Code Online (Sandbox Code Playgroud) 我正在修改纹理合成项目中的一个示例:
use texture_synthesis as ts;
fn main() -> Result<(), ts::Error> {
//create a new session
let texsynth = ts::Session::builder()
//load a single example image
.add_example(&"imgs/1.jpg")
.build()?;
//generate an image
let generated = texsynth.run(None);
//save the image to the disk
generated.save("out/01.jpg")
}
Run Code Online (Sandbox Code Playgroud)
我想用for循环重复这三遍。这就是我认为我可能会这样做的方式:
use texture_synthesis as ts;
fn main() -> Result<(), ts::Error> {
for i in 0..3 {
let texsynth = ts::Session::builder()
//load a single example image
.add_example(&"imgs/1.jpg")
.build()?;
//generate an image
let generated = texsynth.run(None);
//save …Run Code Online (Sandbox Code Playgroud) 我想创建一个函数,它接受x和y坐标值并返回以下格式的字符串(x,y):
pub struct Coord {
x: i32,
y: i32,
}
fn main() {
let my_coord = Coord {
x: 10,
y: 12
};
let my_string = coords(my_coord.x, my_coord.y);
fn coords(x: i32, y: i32) -> &str{
let l = vec!["(", x.to_string(), ",", y.to_string(), ")"];
let j = l.join("");
println!("{}", j);
return &j
}
}
Run Code Online (Sandbox Code Playgroud)
这给了我错误:
|
14 | fn coords(x: i32, y: i32) -> &str {
| ^ expected named lifetime parameter
|
= help: …Run Code Online (Sandbox Code Playgroud) 我试图在定义其他结构的默认值时引用结构的默认值,将默认值按原样嵌套在默认A值B中。在 Rust 中执行此操作的正确方法是什么?
use std::default::Default;
struct A {
val_1: i32,
val_2: i32,
val_3: Vec<String>,
}
impl Default for A {
fn default() -> A {
A {
val_1: 0,
val_2: 0,
val_3: vec!["Hello".to_string()],
}
}
}
struct B {
val_1: i32,
val_2: i32,
val_3: A,
}
impl Default for B {
fn default() -> B {
B {
val_1: 0,
val_2: 0,
val_3: _____ //<---- put the default value for struct A here
}
}
}
Run Code Online (Sandbox Code Playgroud) 我想匹配具有结构值的枚举。当我匹配枚举时,似乎需要为枚举字段提供一个值(如果有)。我想将此值设置为 A::default() 并引用此默认值,但这给了我错误: expected tuple struct or tuple variant, found associated function `A::default。我该如何解决这个问题?操场
use std::default::Default;
struct A {
val_1: i32,
val_2: i32,
val_3: Vec<String>,
}
impl Default for A {
fn default() -> A {
A {
val_1: 0,
val_2: 0,
val_3: vec!["Hello".to_string()],
}
}
}
struct B {
val_1: i32,
val_2: i32,
val_3: A,
}
impl Default for B {
fn default() -> B {
B {
val_1: 0,
val_2: 0,
val_3: A::default(),
}
}
}
enum …Run Code Online (Sandbox Code Playgroud) 如何使用 Rust 中的图像箱确定图像的文件大小?
let img = image::open("imgs/2.jpg").unwrap();
let myBytes = &img.to_bytes();
//get the number of bytes?
Run Code Online (Sandbox Code Playgroud) Cargo.toml
image = "0.23.12"
fltk = "0.10.14"
Run Code Online (Sandbox Code Playgroud)
我想使用 rust 的图像箱将 RGB 数据保存为 jpeg 文件:
use image::{RgbImage};
use image::ImageBuffer;
use fltk::{image::RgbImage, button::*};
let sourceImg = image::open("imgs/2.jpg").unwrap();
let rgb = RgbImage::new(&sourceImg.to_bytes(), x, y, 3).unwrap();
let mut prevWindowImgButton = Button::new(0,0, 300, 300, "");
prevWindowImgButton.set_image(Some(&rgb));
let rgbData= &prevWindowImgButton.image().unwrap().to_rgb_data();
//returns rgb data with type &Vec<u8>
rgbData.save("out/outputtest.jpg");
Run Code Online (Sandbox Code Playgroud)
给出错误:
testRGB.save("out/outputtest.jpg");
| ^^^^ method not found in `&Vec<u8>`
Run Code Online (Sandbox Code Playgroud)
因为 .save 必须在 ImageBuffer 上使用。那么如何将这个 rgb 数据转换为 ImageBuffer 呢?
我正在尝试编写一些自己的调试宏,并正在查看 rusts 构建的源代码dbg!:
macro_rules! dbg {
() => {
$crate::eprintln!("[{}:{}]", $crate::file!(), $crate::line!());
};
($val:expr $(,)?) => {
// Use of `match` here is intentional because it affects the lifetimes
// of temporaries - https://stackoverflow.com/a/48732525/1063961
match $val {
tmp => {
$crate::eprintln!("[{}:{}] {} = {:#?}",
$crate::file!(), $crate::line!(), $crate::stringify!($val), &tmp);
tmp
}
}
};
($($val:expr),+ $(,)?) => {
($($crate::dbg!($val)),+,)
};
}
Run Code Online (Sandbox Code Playgroud)
关于这段代码,有几点让我感到困惑:
$贯穿这段代码的操作符在做什么?($val:expr $(,)?)?我不明白它,是什么以及为什么它在那里。() => {$crate::eprintln!("[{}:{}]", $crate::file!(), $crate::line!());};?假设我有一个基本的 github 操作来构建我的应用程序,然后将其部署到 firebase:
name: Firebase Deploy
on:
push:
branches:
- master
jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@v2.3.2
- name: Install Dependencies
run: npm install
- name: Run Tests
env:
CI: true
run: npm test
- name: Build
run: npm run build
- name: Archive Production Artifact
uses: actions/upload-artifact@v2
with:
name: build
path: build
deploy:
name: Deploy
needs: build
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@v2.3.2
- name: Download Artifact
uses: …Run Code Online (Sandbox Code Playgroud) rust ×7
fp-ts ×2
typescript ×2
formatting ×1
image ×1
javascript ×1
lifetime ×1
reactjs ×1
string ×1