如何在给定字符串中构建ATS循环?

时间:2016-10-26 00:23:51

标签: ats

例如,如何在ATS中编写遍历给定字符串的代码,如下面的C代码所示:

while ((c = *str++) != 0) do_something(c);

3 个答案:

答案 0 :(得分:2)

嗯,总有一个基于组合器的解决方案:

(str).foreach()(lam(c) => do_something(c))

答案 1 :(得分:1)

以下解决方案简单易用,并且不需要任何不安全的功能(但它确实使用了一个高级功能:索引字符串类型)。

fun
loop {n:int}(p0: string(n)): void =
if string_isnot_empty (p0) then let
  val c = (g0ofg1)(string_head(p0))
  val p0 = string_tail(p0)
in
  do_something(c); loop(p0)
end

完整代码:https://glot.io/snippets/ejpwxk2xzx

答案 2 :(得分:0)

以下解决方案使用了UNSAFE,但它应该非常容易访问:

import { TestBed, getTestBed, async, inject } from '@angular/core/testing';
import { Headers, BaseRequestOptions, Response, HttpModule, Http, XHRBackend, RequestMethod} from '@angular/http';

import {ResponseOptions} from '@angular/http';
import {MockBackend, MockConnection} from '@angular/http/testing';
import {FooHttpService} from '../../services/foo-service.service.ts';


describe('My Service Making an Http Call', () => {

    let mockBackend: MockBackend;

    beforeEach(async(() => {
        TestBed.configureTestingModule({
            providers: [
                FooHttpService,
                MockBackend,
                BaseRequestOptions,
                {
                    provide: Http,
                    deps: [MockBackend, BaseRequestOptions],
                    useFactory:
                        (backend: XHRBackend, defaultOptions: BaseRequestOptions) => {
                            return new Http(backend, defaultOptions);
                        }
                }
            ],
            imports: [
                HttpModule
            ]
        });

        mockBackend = getTestBed().get(MockBackend);
        TestBed.compileComponents();
    }));